Skip to content

Commit 32fd879

Browse files
address coderabbit comments
1 parent d1efe24 commit 32fd879

5 files changed

Lines changed: 25 additions & 6 deletions

File tree

main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ async def handler(data):
122122
payload = data.get("data")
123123
peer_addr = data.get("_peer_addr", "unknown")
124124

125+
if payload is None and msg_type in ("hello", "chain_request", "chain_response"):
126+
return
127+
125128
if msg_type == "hello":
126129
peer_chain_id = payload.get("chain_id")
127130
peer_gen_hash = payload.get("genesis_hash")

minichain/contract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _safe_exec_worker(code, globals_dict, context_dict, result_queue, gas_limit)
3838
try:
3939
import resource
4040
# Limit CPU time (seconds) and memory (bytes) - example values
41-
resource.setrlimit(resource.RLIMIT_CPU, (2, 2)) # Align with p.join timeout (2 seconds)
41+
resource.setrlimit(resource.RLIMIT_CPU, (10, 10)) # Align with p.join timeout (10 seconds)
4242
resource.setrlimit(resource.RLIMIT_AS, (100 * 1024 * 1024, 100 * 1024 * 1024))
4343
except ImportError:
4444
logger.warning("Resource module not available. Contract will run without OS-level resource limits.")

minichain/p2p.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def __init__(self, handler_callback=None):
3131
self._seen_block_hashes = set()
3232
self._to_trio = queue.Queue()
3333
self._to_asyncio = queue.Queue()
34+
self._peer_count = 0
35+
self._peer_count_lock = threading.Lock()
3436

3537
def register_handler(self, handler_callback):
3638
self._handler_callback = handler_callback
@@ -96,7 +98,8 @@ async def disconnect_peer(self, peer_addr):
9698

9799
@property
98100
def peer_count(self) -> int:
99-
return 0
101+
with self._peer_count_lock:
102+
return self._peer_count
100103

101104
async def _asyncio_reader(self):
102105
while True:
@@ -132,6 +135,8 @@ async def _trio_main(self):
132135

133136
async def stream_handler(stream):
134137
streams.append(stream)
138+
with self._peer_count_lock:
139+
self._peer_count += 1
135140
peer_id = stream.muxed_conn.peer_id
136141
addr = f"peer:{peer_id}"
137142
self._to_asyncio.put(("PEER_CONNECTED", None))
@@ -147,7 +152,10 @@ async def stream_handler(stream):
147152
self._to_asyncio.put(("MSG", msg))
148153
except Exception: pass
149154
except Exception: pass
150-
if stream in streams: streams.remove(stream)
155+
if stream in streams:
156+
streams.remove(stream)
157+
with self._peer_count_lock:
158+
self._peer_count -= 1
151159

152160
host.set_stream_handler(PROTOCOL_ID, stream_handler)
153161

@@ -185,7 +193,10 @@ async def check_queue():
185193
if addr == arg:
186194
try: await s.reset()
187195
except Exception: pass
188-
if s in streams: streams.remove(s)
196+
if s in streams:
197+
streams.remove(s)
198+
with self._peer_count_lock:
199+
self._peer_count -= 1
189200
except Exception: pass
190201
await trio.sleep(0.1)
191202

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pynacl==1.6.2
22
trie>=3.1.0
33
libp2p
4-
aiohttp
4+
aiohttp>=3.10.11
55
multiaddr

tests/test_contract_transfers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_failed_transfer_out_insufficient_balance(self):
6161
contract_addr = receipt.contract_address
6262

6363
# 2. Call Contract
64-
call_tx = self._sign(Transaction(self.sender_pk, contract_addr, amount=0, nonce=1, data={"target": self.target_pk}, fee=1000))
64+
call_tx = self._sign(Transaction(self.sender_pk, contract_addr, amount=50, nonce=1, data={"target": self.target_pk}, fee=1000))
6565
receipt2 = self.state.apply_transaction(call_tx)
6666

6767
# Should fail with status 0
@@ -72,6 +72,11 @@ def test_failed_transfer_out_insufficient_balance(self):
7272
self.assertEqual(self.state.get_account(contract_addr)['balance'], 100)
7373
self.assertEqual(self.state.get_account(self.target_pk)['balance'], 0)
7474

75+
# Sender's balance should have decreased by only the fee amount (or gas_used if refunded) as the 50 amount was refunded
76+
# Starting balance 10000, minus (100+1000) for deploy = 8900
77+
# Call tx net cost is receipt2.gas_used
78+
self.assertEqual(self.state.get_account(self.sender_pk)['balance'], 8900 - receipt2.gas_used)
79+
7580
# Storage should NOT be updated
7681
self.assertEqual(self.state.get_account(contract_addr)['storage'], {})
7782

0 commit comments

Comments
 (0)