-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
out_point = OutPoint.parse(stream, check_validity) | ||
to_remove.append(out_point) | ||
return cls(hash, to_add, to_remove) | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def add_block(self, block): | ||
block_hash = block.header.hash | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
removed.append((tx_in.prev_out, prevout)) | ||
|
||
for i, tx_out in enumerate(tx.vout): | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
for out_point, tx_out in rev_block.to_add: | ||
self.updated_utxo_set[out_point.serialize(check_validity=False)] = tx_out | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
self.data_dir = data_dir.absolute() / self.chain.name | ||
|
||
self.p2p_port = None | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
for index in sorted(chain_dict.keys()): | ||
self.active_chain.append(chain_dict[index]) | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
block_locators.append(self.header_index[-i]) | ||
if i >= 10: | ||
step *= 2 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
formatter = logging.Formatter("%(asctime)s - %(message)s") | ||
handler.setFormatter(formatter) | ||
self.addHandler(handler) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def add_tx(self, tx): | ||
if not self.is_full(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ def to_ipv6(ip): | |
try: | ||
return IPv6Address(ip) | ||
except AddressValueError: | ||
return IPv6Address("::ffff:" + ip) | ||
return IPv6Address(f"::ffff:{ip}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
@dataclass | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
services = int.from_bytes(stream.read(8), "little") | ||
a = stream.read(16) | ||
ip = IPv6Address(a) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
conn.send(BlockMsg(block)) | ||
|
||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
conn.send(Headers(headers)) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
conn.stop() | ||
except Exception: | ||
conn.stop() | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
conn.stop() | ||
node.logger.debug("Finished p2p\n") | ||
except Exception: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
if len(self.connections) < connection_num and not self.peer_db.is_empty(): | ||
already_connected = [conn.address for conn in self.connections.values()] | ||
try: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return cls(addresses=addresses) | ||
|
||
def serialize(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
tx_index = var_int.parse(stream) | ||
tx = Tx.parse(stream) | ||
prefilled_tx_list.append((tx_index, tx)) | ||
|
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:assign-if-exp
)or-if-exp-identity
)