Skip to content
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

Sourcery refactored master branch #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Jan 2, 2023

Branch master refactored 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 master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from fametrano January 2, 2023 12:45
Comment on lines -50 to +54
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
Copy link
Author

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:

Comment on lines -52 to +57
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)
Copy link
Author

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:

Comment on lines +82 to +85
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
Copy link
Author

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:

if data_dir:
data_dir = Path(data_dir)
else:
data_dir = Path.home() / ".btclib"
data_dir = Path(data_dir) if data_dir else Path.home() / ".btclib"
Copy link
Author

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:

Comment on lines -8 to +51
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]))
Copy link
Author

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:

Comment on lines -113 to +118
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):
Copy link
Author

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:

Comment on lines -126 to +130
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):
Copy link
Author

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:

Comment on lines -152 to +149
else:
if node.status == NodeStatus.SyncingHeaders:
node.status = NodeStatus.HeaderSynced
elif node.status == NodeStatus.SyncingHeaders:
node.status = NodeStatus.HeaderSynced
Copy link
Author

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:

Comment on lines -159 to +156
headers = node.index.get_headers_from_locators(
if headers := node.index.get_headers_from_locators(
getheaders.block_hashes, getheaders.hash_stop
)
if headers:
):
Copy link
Author

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:

elif conn.status == P2pConnStatus.Closed:
pass
else:
elif conn.status != P2pConnStatus.Closed:
Copy link
Author

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:

Comment on lines -31 to +29
elif conn.status == P2pConnStatus.Closed:
pass
else:
elif conn.status != P2pConnStatus.Closed:
Copy link
Author

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:

if self.node.status < NodeStatus.HeaderSynced:
connection_num = 1
else:
connection_num = 10
connection_num = 1 if self.node.status < NodeStatus.HeaderSynced else 10
Copy link
Author

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:

addresses = []
for x in range(len_addresses):
addresses.append(NetworkAddress.deserialize(stream))
addresses = [NetworkAddress.deserialize(stream) for _ in range(len_addresses)]
Copy link
Author

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:

Comment on lines -43 to +47
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):
Copy link
Author

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:

for x in range(headers_num):
for _ in range(headers_num):
Copy link
Author

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:

Comment on lines -60 to +58
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"
Copy link
Author

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:

conn = manager.connections[id]
return conn
return manager.connections[id]
Copy link
Author

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:

Comment on lines -15 to +14
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
Copy link
Author

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:

Comment on lines -54 to +49
if "params" in request:
params = request["params"]
else:
params = []
params = request["params"] if "params" in request else []
Copy link
Author

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:

if chain:
previous_block_hash = chain[-1].hash
else:
previous_block_hash = start
previous_block_hash = chain[-1].hash if chain else start
Copy link
Author

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:

for x in range(len(chain)):
for _ in range(len(chain)):
Copy link
Author

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:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Jan 2, 2023

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.89%.

Quality metrics Before After Change
Complexity 5.39 ⭐ 4.64 ⭐ -0.75 👍
Method Length 59.30 ⭐ 58.65 ⭐ -0.65 👍
Working memory 7.33 🙂 7.26 🙂 -0.07 👍
Quality 71.88% 🙂 72.77% 🙂 0.89% 👍
Other metrics Before After Change
Lines 1772 1710 -62
Changed files Quality Before Quality After Quality Change
btclib_node/__init__.py 57.78% 🙂 60.00% 🙂 2.22% 👍
btclib_node/chainstate.py 55.00% 🙂 55.64% 🙂 0.64% 👍
btclib_node/config.py 35.55% 😞 36.51% 😞 0.96% 👍
btclib_node/download.py 25.79% 😞 40.20% 😞 14.41% 👍
btclib_node/log.py 86.42% ⭐ 86.77% ⭐ 0.35% 👍
btclib_node/mempool.py 88.50% ⭐ 89.43% ⭐ 0.93% 👍
btclib_node/block_db/__init__.py 77.98% ⭐ 78.06% ⭐ 0.08% 👍
btclib_node/index/__init__.py 69.20% 🙂 69.24% 🙂 0.04% 👍
btclib_node/p2p/address.py 82.24% ⭐ 82.72% ⭐ 0.48% 👍
btclib_node/p2p/callbacks.py 72.24% 🙂 73.56% 🙂 1.32% 👍
btclib_node/p2p/main.py 59.11% 🙂 60.17% 🙂 1.06% 👍
btclib_node/p2p/manager.py 70.35% 🙂 71.29% 🙂 0.94% 👍
btclib_node/p2p/messages/address.py 90.62% ⭐ 91.17% ⭐ 0.55% 👍
btclib_node/p2p/messages/compact.py 75.43% ⭐ 76.71% ⭐ 1.28% 👍
btclib_node/p2p/messages/data.py 87.28% ⭐ 87.47% ⭐ 0.19% 👍
btclib_node/p2p/messages/errors.py 86.59% ⭐ 86.59% ⭐ 0.00%
btclib_node/p2p/messages/getdata.py 85.32% ⭐ 85.41% ⭐ 0.09% 👍
btclib_node/p2p/messages/ping.py 91.62% ⭐ 92.63% ⭐ 1.01% 👍
btclib_node/rpc/callbacks.py 70.59% 🙂 73.75% 🙂 3.16% 👍
btclib_node/rpc/connection.py 77.10% ⭐ 76.25% ⭐ -0.85% 👎
btclib_node/rpc/main.py 72.80% 🙂 71.62% 🙂 -1.18% 👎
tests/helpers.py 72.25% 🙂 72.42% 🙂 0.17% 👍
tests/unit/main.py 63.96% 🙂 66.31% 🙂 2.35% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
btclib_node/chainstate.py Chainstate.add_block 17 🙂 246 ⛔ 17 ⛔ 29.83% 😞 Try splitting into smaller methods. Extract out complex expressions
btclib_node/p2p/manager.py P2pManager.manage_connections 29 😞 193 😞 10 😞 35.57% 😞 Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
btclib_node/config.py Config.__init__ 16 🙂 176 😞 16 ⛔ 36.51% 😞 Try splitting into smaller methods. Extract out complex expressions
btclib_node/download.py block_download 15 🙂 207 ⛔ 12 😞 40.20% 😞 Try splitting into smaller methods. Extract out complex expressions
btclib_node/index/__init__.py BlockIndex.add_headers 10 🙂 171 😞 12 😞 47.50% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants