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
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
10 changes: 2 additions & 8 deletions btclib_node/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,11 @@ def sigint_handler(signal, frame):

self.download_window = []

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
Comment on lines -50 to +54
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:

self.rpc_manager = RpcManager(self, self.rpc_port)

def run(self):
Expand Down
7 changes: 3 additions & 4 deletions btclib_node/block_db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def deserialize(cls, data, check_validity=False):
stream = bytesio_from_binarydata(data)
hash = stream.read(32)
to_add = []
for x in range(var_int.parse(stream)):
for _ in range(var_int.parse(stream)):
out_point = OutPoint.parse(stream, check_validity)
tx_out = TxOut.parse(stream, check_validity)
to_add.append([out_point, tx_out])
to_remove = []
for x in range(var_int.parse(stream)):
for _ in range(var_int.parse(stream)):
Comment on lines -23 to +28
Copy link
Author

Choose a reason for hiding this comment

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

Function RevBlock.deserialize refactored with the following changes:

out_point = OutPoint.parse(stream, check_validity)
to_remove.append(out_point)
return cls(hash, to_add, to_remove)
Expand Down Expand Up @@ -175,8 +175,7 @@ def __add_data_to_file(self, file, data):

def __get_data_from_file(self, file, index, size):
file.seek(index)
data = file.read(size)
return data
return file.read(size)
Copy link
Author

Choose a reason for hiding this comment

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

Function BlockDB.__get_data_from_file refactored with the following changes:


def add_block(self, block):
block_hash = block.header.hash
Expand Down
16 changes: 7 additions & 9 deletions btclib_node/chainstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ def add_block(self, block):
self.updated_utxo_set.pop(prevout_bytes)
else:
prevout = self.db.get(prevout_bytes)
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)
Comment on lines -52 to +57
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:

removed.append((tx_in.prev_out, prevout))

for i, tx_out in enumerate(tx.vout):
Expand All @@ -80,11 +79,10 @@ def apply_rev_block(self, rev_block):
raise Exception
if out_point_bytes in self.updated_utxo_set:
self.updated_utxo_set.pop(out_point_bytes)
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
Comment on lines +82 to +85
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:


for out_point, tx_out in rev_block.to_add:
self.updated_utxo_set[out_point.serialize(check_validity=False)] = tx_out
Expand Down
5 changes: 1 addition & 4 deletions btclib_node/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ def __init__(
else:
raise ValueError

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:

self.data_dir = data_dir.absolute() / self.chain.name

self.p2p_port = None
Expand Down
79 changes: 40 additions & 39 deletions btclib_node/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,47 @@


def block_download(node):
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]))
Comment on lines -8 to +51
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:

15 changes: 7 additions & 8 deletions btclib_node/index/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ def calculate_chainwork(self):
block_info.chainwork = old_work + calculate_work(block_info.header)

def generate_active_chain(self):
chain_dict = {}
for block_hash, block_info in self.header_dict.items():
if block_info.status == BlockStatus.in_active_chain:
chain_dict[block_info.index] = block_hash
chain_dict = {
block_info.index: block_hash
for block_hash, block_info in self.header_dict.items()
if block_info.status == BlockStatus.in_active_chain
}
Comment on lines -113 to +117
Copy link
Author

Choose a reason for hiding this comment

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

Function BlockIndex.generate_active_chain refactored with the following changes:

for index in sorted(chain_dict.keys()):
self.active_chain.append(chain_dict[index])

Expand Down Expand Up @@ -264,17 +265,15 @@ def get_download_candidates(self):
break
if not block_info.downloaded:
new_candidates.append(candidate)
candidates = candidates + new_candidates[::-1]
candidates += new_candidates[::-1]
Comment on lines -267 to +268
Copy link
Author

Choose a reason for hiding this comment

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

Function BlockIndex.get_download_candidates refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)

return candidates[:1024]

# return a list of block hashes looking at the current best chain
def get_block_locator_hashes(self):
i = 1
step = 1
block_locators = []
while True:
if i > len(self.header_index):
break
while i <= len(self.header_index):
Comment on lines -275 to +276
Copy link
Author

Choose a reason for hiding this comment

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

Function BlockIndex.get_block_locator_hashes refactored with the following changes:

block_locators.append(self.header_index[-i])
if i >= 10:
step *= 2
Expand Down
5 changes: 1 addition & 4 deletions btclib_node/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
class Logger(logging.Logger):
def __init__(self, filepath=None, debug=False, **kwargs):
super().__init__(name="Logger", level=logging.DEBUG, **kwargs)
if debug:
handler = logging.StreamHandler()
else:
handler = logging.FileHandler(filepath)
handler = logging.StreamHandler() if debug else logging.FileHandler(filepath)
Copy link
Author

Choose a reason for hiding this comment

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

Function Logger.__init__ refactored with the following changes:

formatter = logging.Formatter("%(asctime)s - %(message)s")
handler.setFormatter(formatter)
self.addHandler(handler)
Expand Down
8 changes: 2 additions & 6 deletions btclib_node/mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ def is_full(self):
def get_missing(self, transactions):
missing = []
if not self.is_full():
for tx in transactions:
if tx not in self.transactions:
missing.append(tx)
missing.extend(tx for tx in transactions if tx not in self.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 Mempool.get_missing refactored with the following changes:

return missing

def get_tx(self, txid):
if txid in self.transactions:
return self.transactions[txid]
return None
return self.transactions[txid] if txid in self.transactions else None
Comment on lines -24 to +22
Copy link
Author

Choose a reason for hiding this comment

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

Function Mempool.get_tx refactored with the following changes:


def add_tx(self, tx):
if not self.is_full():
Expand Down
7 changes: 2 additions & 5 deletions btclib_node/p2p/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def to_ipv6(ip):
try:
return IPv6Address(ip)
except AddressValueError:
return IPv6Address("::ffff:" + ip)
return IPv6Address(f"::ffff:{ip}")
Copy link
Author

Choose a reason for hiding this comment

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

Function to_ipv6 refactored with the following changes:



@dataclass
Expand All @@ -24,10 +24,7 @@ class NetworkAddress:
@classmethod
def deserialize(cls, data, version_msg=False):
stream = bytesio_from_binarydata(data)
if not version_msg:
time = int.from_bytes(stream.read(4), "little")
else:
time = 0
time = 0 if version_msg else int.from_bytes(stream.read(4), "little")
Copy link
Author

Choose a reason for hiding this comment

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

Function NetworkAddress.deserialize refactored with the following changes:

services = int.from_bytes(stream.read(8), "little")
a = stream.read(16)
ip = IPv6Address(a)
Expand Down
28 changes: 11 additions & 17 deletions btclib_node/p2p/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,24 @@ def inv(node, msg, conn):
if node.status < NodeStatus.BlockSynced:
return

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

conn.send(Getdata([(0x40000001, tx) for tx in missing_tx]))


def getdata(node, msg, conn):
getdata = Getdata.deserialize(msg)
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):
Comment on lines -126 to +130
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:

conn.send(BlockMsg(block))


Expand All @@ -149,17 +145,15 @@ def headers(node, msg, conn):
if len(headers) == 2000 and added: # we have to require more headers
block_locators = node.index.get_block_locator_hashes()
conn.send(Getheaders(ProtocolVersion, block_locators, b"\x00" * 32))
else:
if node.status == NodeStatus.SyncingHeaders:
node.status = NodeStatus.HeaderSynced
elif node.status == NodeStatus.SyncingHeaders:
node.status = NodeStatus.HeaderSynced
Comment on lines -152 to +149
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:



def getheaders(node, msg, conn):
getheaders = Getheaders.deserialize(msg)
headers = node.index.get_headers_from_locators(
if headers := node.index.get_headers_from_locators(
getheaders.block_hashes, getheaders.hash_stop
)
if headers:
):
Comment on lines -159 to +156
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:

conn.send(Headers(headers))


Expand Down
8 changes: 2 additions & 6 deletions btclib_node/p2p/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ def handle_p2p_handshake(node):
try:
if conn.status == P2pConnStatus.Open:
handshake_callbacks[msg_type](node, msg, conn)
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:

conn.stop()
except Exception:
conn.stop()
Expand All @@ -28,9 +26,7 @@ def handle_p2p(node):
if msg_type in callbacks:
if conn.status == P2pConnStatus.Connected:
callbacks[msg_type](node, msg, conn)
elif conn.status == P2pConnStatus.Closed:
pass
else:
elif conn.status != P2pConnStatus.Closed:
Comment on lines -31 to +29
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:

conn.stop()
node.logger.debug("Finished p2p\n")
except Exception:
Expand Down
5 changes: 1 addition & 4 deletions btclib_node/p2p/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ async def manage_connections(self, loop):
conn.ping_nonce = ping_msg.nonce
elif now - conn.ping_sent > 120:
self.remove_connection(conn.id)
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:

if len(self.connections) < connection_num and not self.peer_db.is_empty():
already_connected = [conn.address for conn in self.connections.values()]
try:
Expand Down
4 changes: 1 addition & 3 deletions btclib_node/p2p/messages/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class Addr:
def deserialize(cls, data):
stream = bytesio_from_binarydata(data)
len_addresses = var_int.parse(stream)
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:

return cls(addresses=addresses)

def serialize(self):
Expand Down
6 changes: 2 additions & 4 deletions btclib_node/p2p/messages/compact.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ def deserialize(cls, data):
stream = bytesio_from_binarydata(data)
header = BlockHeader.parse(stream)
nonce = int.from_bytes(stream.read(8), "little")
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):
Comment on lines -43 to +47
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:

tx_index = var_int.parse(stream)
tx = Tx.parse(stream)
prefilled_tx_list.append((tx_index, tx))
Expand Down
Loading