Skip to content

Commit be24050

Browse files
committed
feat: Implement Bitswap 1.3.0 Payment Extension
- Added PaymentExtension class to handle payment-related protobuf fields and wantlists in Bitswap 1.3.0. - Integrated payment terms, receipts, and rejections processing for client-side and server-side. - Enhanced PaymentLedger to track root CID payments and manage payment records. - Updated pricing engine to support configurable pricing strategies. - Refactored tests to accommodate changes in block storage and encoding, ensuring raw blocks are used for leaves. - Improved type hints and documentation across the codebase for better clarity and maintainability.
1 parent caa4267 commit be24050

17 files changed

Lines changed: 1639 additions & 1383 deletions

examples/bitswap/bitswap.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
from libp2p import new_host
1717
from libp2p.bitswap import BitswapClient
18-
from libp2p.crypto.ed25519 import create_new_key_pair
1918
from libp2p.bitswap.cid import cid_to_bytes, format_cid_for_display
2019
from libp2p.bitswap.dag import MerkleDag
20+
from libp2p.crypto.ed25519 import create_new_key_pair
2121
from libp2p.peer.peerinfo import info_from_p2p_addr
2222
from libp2p.utils.address_validation import (
2323
find_free_port,
@@ -40,9 +40,7 @@
4040
DEFAULT_LISTEN_PORT = 4013
4141

4242

43-
def select_preferred_listen_addr(
44-
addrs: list[Multiaddr], port: int
45-
) -> Multiaddr:
43+
def select_preferred_listen_addr(addrs: list[Multiaddr], port: int) -> Multiaddr:
4644
"""Pick a stable, local-friendly address for copy/paste commands."""
4745
preferred_v4 = f"/ip4/127.0.0.1/tcp/{port}"
4846
for addr in addrs:
@@ -102,7 +100,7 @@ async def run_provider(file_path: str, port: int = 0, seed: str | None = None):
102100
# Convert seed string to bytes (must be 32 bytes for Ed25519)
103101
seed_bytes = hashlib.sha256(seed.encode()).digest()
104102
key_pair = create_new_key_pair(seed=seed_bytes)
105-
logger.info(f"Using deterministic peer ID from seed")
103+
logger.info("Using deterministic peer ID from seed")
106104

107105
host = new_host(key_pair=key_pair)
108106

@@ -164,9 +162,7 @@ def progress_callback(current: int, total: int, status: str):
164162
# Prefer a deterministic local address for copy/paste commands.
165163
transport_addrs = host.get_transport_addrs()
166164
provider_addr = select_preferred_listen_addr(transport_addrs, port)
167-
provider_addr = provider_addr.encapsulate(
168-
Multiaddr(f"/p2p/{host.get_id()}")
169-
)
165+
provider_addr = provider_addr.encapsulate(Multiaddr(f"/p2p/{host.get_id()}"))
170166
root_cid_text = format_cid_for_display(root_cid)
171167
logger.info(f"Root CID: {root_cid_text}")
172168
logger.info("")
@@ -237,7 +233,7 @@ async def run_client(
237233
# Convert seed string to bytes (must be 32 bytes for Ed25519)
238234
seed_bytes = hashlib.sha256(seed.encode()).digest()
239235
key_pair = create_new_key_pair(seed=seed_bytes)
240-
logger.info(f"Using deterministic peer ID from seed")
236+
logger.info("Using deterministic peer ID from seed")
241237

242238
host = new_host(key_pair=key_pair)
243239

@@ -379,10 +375,7 @@ def parse_args():
379375
"--port",
380376
type=int,
381377
default=DEFAULT_LISTEN_PORT,
382-
help=(
383-
"Port to listen on (default: 4012). "
384-
"Use 0 to auto-select a random port."
385-
),
378+
help=("Port to listen on (default: 4012). Use 0 to auto-select a random port."),
386379
)
387380
parser.add_argument(
388381
"--file",
@@ -416,7 +409,10 @@ def parse_args():
416409
parser.add_argument(
417410
"--seed",
418411
type=str,
419-
help="Seed string for deterministic peer ID generation (same seed = same peer ID)",
412+
help=(
413+
"Seed string for deterministic peer ID generation "
414+
"(same seed = same peer ID)"
415+
),
420416
)
421417

422418
args = parser.parse_args()
@@ -450,7 +446,9 @@ def main():
450446
if args.mode == "provider":
451447
trio.run(run_provider, args.file, args.port, args.seed)
452448
elif args.mode == "client":
453-
trio.run(run_client, args.provider, args.cid, args.output, args.port, args.seed)
449+
trio.run(
450+
run_client, args.provider, args.cid, args.output, args.port, args.seed
451+
)
454452
except Exception as e:
455453
logger.critical(f"Script failed: {e}", exc_info=True)
456454
sys.exit(1)

libp2p/bitswap/client.py

Lines changed: 133 additions & 226 deletions
Large diffs are not rendered by default.

libp2p/bitswap/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
# All supported protocols (ordered from newest to oldest for negotiation)
1414
BITSWAP_PROTOCOLS = [
15-
BITSWAP_PROTOCOL_V130,
1615
BITSWAP_PROTOCOL_V120,
1716
BITSWAP_PROTOCOL_V110,
1817
BITSWAP_PROTOCOL_V100,

0 commit comments

Comments
 (0)