Skip to main content

A unified Python SDK for P2P networking with integrated DHT and NAT capabilities

Project description

Lattica Python SDK

A unified Python SDK for P2P networking with integrated DHT and NAT capabilities, built on top of libp2p.

Features

  • Distributed Hash Table (DHT): Store and retrieve key-value pairs across the network
  • Remote Procedure Call (RPC): Execute remote functions with support for complex data types
  • Streaming RPC: Handle large data transfers with streaming capabilities
  • NAT Traversal: Automatic NAT traversal with UPnP support
  • Peer Discovery: mDNS and rendezvous-based peer discovery
  • High Performance: Built with Rust for optimal performance

Installation

You can install the released versions

pip install lattica

Or you can install from source using:

pip install git+https://github.com/GradientHQ/lattica.git#subdirectory=bindings/python

Quick Start

Basic Usage

from lattica import Lattica

# Create a Lattica instance
lattica = Lattica.builder().build()

# Get your peer ID
peer_id = lattica.peer_id()
print(f"My peer ID: {peer_id}")

Examples

1. DHT Operations

The DHT example demonstrates basic key-value storage and retrieval with subkey support.

from lattica import Lattica

# Create client1 as bootstrap node
lattica = Lattica.builder()
    .build()

# Create client2 with bootstrap nodes
lattica = Lattica.builder() \
    .with_bootstraps(["/ip4/127.0.0.1/tcp/54282/p2p/QmServerPeerId"]) \
    .build()

# Store a simple key-value pair with default expiration 10 minute
lattica.store("name", "alice")

# get the value
result = lattica.get("name")
if result:
    print(f"Value: {result.value}")
    print(f"Expires: {result.expiration_time}")


# Store values with subkeys (useful for voting, user data, etc.)
key = "peer_list"
peers = ["alice", "bob", "carol"]

# Each peer stores their vote with a subkey
lattica.store(key, "yes", expiration_time, subkey="alice")
lattica.store(key, "no", expiration_time, subkey="bob")
lattica.store(key, "maybe", expiration_time, subkey="carol")

# get all votes
votes_result = lattica.get(key)
if votes_result:
    for peer, vote_info in votes_result.value.items():
        print(f"{peer}: {vote_info.value}")

2. RPC Operations

The RPC example demonstrates remote procedure calls with support for complex data types and streaming.

from lattica import Lattica, rpc_method, rpc_stream, rpc_stream_iter, ConnectionHandler



class MyService(ConnectionHandler):
    @rpc_method
    def add(self, a: int, b: int) -> int:
        """Simple addition"""
        return a + b
    
    @rpc_stream
    def process_data(self, data: list) -> list:
        return data

    @rpc_stream_iter
    def stream_rpc_iter(self):
        while True:
            text = "hello world"
            yield text
            
# Create client1 as RPC server and bootstrap node
lattica = Lattica.builder()
    .build()
service = MyService(lattica)

# Create client2 with bootstrap nodes
lattica = Lattica.builder() \
    .with_bootstraps(["/ip4/127.0.0.1/tcp/54282/p2p/QmServerPeerId"]) \
    .build()
client_service = MyService(client_lattica)

# Make RPC calls
stub = client_service.get_stub(server_peer_id)
result = stub.add(10, 20)  # Returns 30

# Handle complex data types
num_floats = int(2 * 1024 * 1024 * 1024) // 8 #2GB
test_data = [random.random() for _ in range(num_floats)]
result = stub.process_data(test_data)

# stream iter call
for text in stub.stream_rpc_iter():
    print(f"recv: {text}")

Configuration

Builder Pattern

lattica = Lattica.builder() \
    .with_bootstraps([
        "/ip4/127.0.0.1/tcp/8080/p2p/QmBootstrap1",
        "/ip4/127.0.0.1/tcp/8081/p2p/QmBootstrap2"
    ]) \
    .with_listen_addrs(["/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/udp/0/quic-v1"])
    .with_external_addrs(["/ip4/0.0.0.0/tcp/0"])
    .with_mdns(True) \
    .with_upnp(True) \
    .build()

Configuration Options

  • with_bootstraps(nodes): Set bootstrap nodes for network discovery
  • with_listen_addrs(addrs): Set listening address
  • with_mdns(enabled): Enable/disable mDNS peer discovery
  • with_upnp(enabled): Enable/disable UPnP NAT traversal
  • with_relay_servers(servers): Set relay servers for network relay
  • with_autonat(enabled): Enable/disable AutoNAT detect[need relay servers]
  • with_dcutr(enabled): Enable/disable TCP/QUIC NAT travelsal[need relay servers]
  • with_external_addrs(addrs): Set external address
  • with_storage_path: Persistent storage path
  • with_dht_db_path: DHT Persistent db path
  • with_key_path: Set Keypair path

Development

Building from Source

# Install maturin
pip install maturin

# Build the package
cd bindings/python
pip install .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

lattica_dev-1.0.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.7 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

lattica_dev-1.0.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.0 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ ARM64

lattica_dev-1.0.1-pp39-pypy39_pp73-macosx_10_12_x86_64.whl (8.5 MB view details)

Uploaded PyPymacOS 10.12+ x86-64

lattica_dev-1.0.1-cp38-abi3-win_amd64.whl (7.7 MB view details)

Uploaded CPython 3.8+Windows x86-64

lattica_dev-1.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.7 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

lattica_dev-1.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (11.0 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

lattica_dev-1.0.1-cp38-abi3-macosx_11_0_arm64.whl (8.2 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file lattica_dev-1.0.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lattica_dev-1.0.1-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c1551085fc533aee31e2ecb6d8bdc5ad07f5ae7d70521b1f916c553de772eb94
MD5 90c5ac193195c66cb7fd11858a754f93
BLAKE2b-256 87677951a28f45c7faec1c5b0645bf374440b3e370ea5b697d0edd06a301f19b

See more details on using hashes here.

File details

Details for the file lattica_dev-1.0.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for lattica_dev-1.0.1-pp311-pypy311_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1da4c94fd9a83ba555baf7c9e5b9021c7ccd9758691133dcff66ced9cffa1938
MD5 f64b1866b912a0795d820022ccdbd619
BLAKE2b-256 8aa89c19c42c6418e17ca07059b2c2d823f013a8d07f49e3323ed7cc4cbe3aad

See more details on using hashes here.

File details

Details for the file lattica_dev-1.0.1-pp39-pypy39_pp73-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for lattica_dev-1.0.1-pp39-pypy39_pp73-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 423942b85bdce70360c0a8cf1a8d5a69091cf08ca5f35fe79c5a15d577182a5e
MD5 fa463a5a87a058a553a720e26ceb9145
BLAKE2b-256 22e75e4ef58a409e4a897a3e7c8991b36ad7096a416fe6893f9a172a1f4ac6d0

See more details on using hashes here.

File details

Details for the file lattica_dev-1.0.1-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: lattica_dev-1.0.1-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 7.7 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for lattica_dev-1.0.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f3a4f276468985e29b744229fca37cf6b921083761543d2a9e61360c1e19bc4c
MD5 68a86fb214d6c46a9bdbb34411ba53cd
BLAKE2b-256 b63c2eb22c9376632056f7c91b65d3605ff468b8b16a53c7504f8f241ebba5b5

See more details on using hashes here.

File details

Details for the file lattica_dev-1.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lattica_dev-1.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bebc458a2b43ddeb577c67c23ca712355cc8f4e55750d17a163830747b164406
MD5 f4e41d9b3bc3035fc6b37fb717330ce5
BLAKE2b-256 3274d196a907d5d6b31099e7867eb359a514cf234382458ea32ead2cb75ca6ee

See more details on using hashes here.

File details

Details for the file lattica_dev-1.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for lattica_dev-1.0.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f4c49bd4427a1627e2501a6bee8321da94182021f50fe7452f43c65083a03448
MD5 8f5528090a9a4e4f343031f59f3f5889
BLAKE2b-256 838be66177e0542019a570286eee20d4632543e295391bc74f13d3532ffa61b5

See more details on using hashes here.

File details

Details for the file lattica_dev-1.0.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for lattica_dev-1.0.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f11932152d8d3f48f87a2003727e65e2d3fecfae7596afe683a2231e7a137e2c
MD5 22d954bb81ae4e2f2d14048cb77a369f
BLAKE2b-256 fa72e943369c4445c8dd652dad326c284ce10f620a61f1f0334b033e7f65f0d8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page