Skip to content

Commit 8e27505

Browse files
committed
fix type annotations and context manager signatures in persistent peerstore
1 parent 4ec66a7 commit 8e27505

File tree

6 files changed

+58
-18
lines changed

6 files changed

+58
-18
lines changed

libp2p/peer/persistent/async_/peerstore.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,10 @@ async def __aenter__(self) -> "AsyncPersistentPeerStore":
856856
return self
857857

858858
async def __aexit__(
859-
self, exc_type: type, exc_val: Exception, exc_tb: object
859+
self,
860+
exc_type: type[BaseException] | None,
861+
exc_val: BaseException | None,
862+
exc_tb: object,
860863
) -> None:
861864
"""Async context manager exit."""
862865
await self.close_async()

libp2p/peer/persistent/datastore/leveldb_sync.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,15 @@ def close(self) -> None:
184184
self.db.close()
185185
self.db = None
186186

187-
def __enter__(self):
187+
def __enter__(self) -> "LevelDBDatastoreSync":
188188
"""Context manager entry."""
189189
return self
190190

191-
def __exit__(self, exc_type, exc_val, exc_tb):
191+
def __exit__(
192+
self,
193+
exc_type: type[BaseException] | None,
194+
exc_val: BaseException | None,
195+
exc_tb: object,
196+
) -> None:
192197
"""Context manager exit."""
193198
self.close()
194-
return False

libp2p/peer/persistent/datastore/rocksdb_sync.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,15 @@ def close(self) -> None:
194194
self.db.close()
195195
self.db = None
196196

197-
def __enter__(self):
197+
def __enter__(self) -> "RocksDBDatastoreSync":
198198
"""Context manager entry."""
199199
return self
200200

201-
def __exit__(self, exc_type, exc_val, exc_tb):
201+
def __exit__(
202+
self,
203+
exc_type: type[BaseException] | None,
204+
exc_val: BaseException | None,
205+
exc_tb: object,
206+
) -> None:
202207
"""Context manager exit."""
203208
self.close()
204-
return False

libp2p/peer/persistent/datastore/sqlite_sync.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,16 @@ def close(self) -> None:
217217
self.connection = None
218218
self._closed = True
219219

220-
def __enter__(self):
220+
def __enter__(self) -> "SQLiteDatastoreSync":
221221
"""Context manager entry."""
222222
self._ensure_connection()
223223
return self
224224

225-
def __exit__(self, exc_type, exc_val, exc_tb):
225+
def __exit__(
226+
self,
227+
exc_type: type[BaseException] | None,
228+
exc_val: BaseException | None,
229+
exc_tb: object,
230+
) -> None:
226231
"""Context manager exit."""
227232
self.close()
228-
return False

libp2p/peer/persistent/serialization.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
from libp2p.crypto.keys import KeyPair
1515
from libp2p.peer.envelope import Envelope
16+
from libp2p.peer.pb.crypto_pb2 import (
17+
KeyType as PBKeyType,
18+
PrivateKey as PBPrivateKey,
19+
PublicKey as PBPublicKey,
20+
)
1621
from libp2p.peer.peerstore import PeerRecordState
1722

1823
from .pb import (
@@ -151,24 +156,39 @@ def serialize_keypair(keypair: KeyPair) -> bytes:
151156

152157
# Serialize public key
153158
if keypair.public_key:
154-
from libp2p.peer.pb.crypto_pb2 import PublicKey as PBPublicKey
155-
156159
pb_public_key = PBPublicKey()
157160

158-
# Map key types
159161
key_type = keypair.public_key.get_type()
160-
pb_public_key.Type = key_type.value
162+
# Map from libp2p KeyType to protobuf KeyType
163+
if key_type.value == 0: # RSA
164+
pb_public_key.Type = PBKeyType.RSA
165+
elif key_type.value == 1: # Ed25519
166+
pb_public_key.Type = PBKeyType.Ed25519
167+
elif key_type.value == 2: # Secp256k1
168+
pb_public_key.Type = PBKeyType.Secp256k1
169+
elif key_type.value == 3: # ECDSA
170+
pb_public_key.Type = PBKeyType.ECDSA
171+
else:
172+
raise SerializationError(f"Unsupported key type: {key_type}")
161173
pb_public_key.Data = keypair.public_key.serialize()
162174
pb_keys.public_key.CopyFrom(pb_public_key)
163175

164176
# Serialize private key
165177
if keypair.private_key:
166-
from libp2p.peer.pb.crypto_pb2 import PrivateKey as PBPrivateKey
167-
168178
pb_private_key = PBPrivateKey()
169179

170180
key_type = keypair.private_key.get_type()
171-
pb_private_key.Type = key_type.value
181+
# Map from libp2p KeyType to protobuf KeyType
182+
if key_type.value == 0: # RSA
183+
pb_private_key.Type = PBKeyType.RSA
184+
elif key_type.value == 1: # Ed25519
185+
pb_private_key.Type = PBKeyType.Ed25519
186+
elif key_type.value == 2: # Secp256k1
187+
pb_private_key.Type = PBKeyType.Secp256k1
188+
elif key_type.value == 3: # ECDSA
189+
pb_private_key.Type = PBKeyType.ECDSA
190+
else:
191+
raise SerializationError(f"Unsupported key type: {key_type}")
172192
pb_private_key.Data = keypair.private_key.serialize()
173193
pb_keys.private_key.CopyFrom(pb_private_key)
174194

libp2p/peer/persistent/sync/peerstore.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,11 @@ def __enter__(self) -> "SyncPersistentPeerStore":
814814
"""Context manager entry."""
815815
return self
816816

817-
def __exit__(self, exc_type: type, exc_val: Exception, exc_tb: object) -> None:
817+
def __exit__(
818+
self,
819+
exc_type: type[BaseException] | None,
820+
exc_val: BaseException | None,
821+
exc_tb: object,
822+
) -> None:
818823
"""Context manager exit."""
819824
self.close()

0 commit comments

Comments
 (0)