GithubHelp home page GithubHelp logo

swift-libp2p / swift-libp2p-kad-dht Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 293 KB

A Kademlia based Distributed Hash Table Implementation for Libp2p

Swift 100.00%
dht distributed-hash-table kademlia kademlia-dht libp2p p2p

swift-libp2p-kad-dht's Introduction

LibP2PKadDHT

Swift Package Manager compatible Build & Test (macos and linux)

A Kademlia Distributed Hash Table for LibP2P

Table of Contents

Overview

The Kademlia Distributed Hash Table (DHT) subsystem in libp2p is a DHT implementation largely based on the Kademlia [0] whitepaper, augmented with notions from S/Kademlia [1], Coral [2] and the BitTorrent DHT.

DHT operations

The libp2p Kademlia DHT offers the following types of operations:

  • Peer routing

    • Finding the closest nodes to a given key via FIND_NODE.
  • Value storage and retrieval

    • Storing a value on the nodes closest to the value's key by looking up the closest nodes via FIND_NODE and then putting the value to those nodes via PUT_VALUE.

    • Getting a value by its key from the nodes closest to that key via GET_VALUE.

  • Content provider advertisement and discovery

    • Adding oneself to the list of providers for a given key at the nodes closest to that key by finding the closest nodes via FIND_NODE and then adding oneself via ADD_PROVIDER.

    • Getting providers for a given key from the nodes closest to that key via GET_PROVIDERS.

In addition the libp2p Kademlia DHT offers the auxiliary bootstrap operation.

Note:

Install

Include the following dependency in your Package.swift file

let package = Package(
    ...
    dependencies: [
        ...
        .package(name: "LibP2PKadDHT", url: "https://github.com/swift-libp2p/swift-libp2p-kad-dht.git", .upToNextMajor(from: "0.0.1"))
    ],
    
    ...
)

Usage

Example

check out the tests for more examples

import LibP2PKadDHT

let app = Application(.detect())

/// If you'd like to use the DHT as a DHT...
app.dht.use( .kadDHT )

/// Or if you're just interested in its peer discovery functionality
app.discovery.use(.kadDHT)

API

extension StorageKeys {
    static let MyDHT = "MyDHT"
}

let dht = BasicKadDHT(protocolPrefix: "mydht", version: "1.0.0", k: 20, alpha: 4, peerstore: .shared(app.peerstore) ?? .basicInMemory, kvstore: .basicInMemory...)
app.use(dht, id: .MyDHT)

dht.handle(namespace: "pk", valueAs: PublicKey.self, withValidator: { msg in msg.record.value.multihash == msg.key && msg.key }).onPutSuccess({ msg in ... }).onPutFailed({ msg, error in ...})

dht.handle(namespace: "ipfs", valueAs: IPNSRecord.self, withValidator: { msg in ... }).onPutSuccess({ msg in ... }).onPutFailed({ msg, error in ...})

// ... or ...
app.dht.group("myDHT") { myDHT in
    myDHT.on("pk", validator: ...) { pk in ... }
    myDHT.on("ipld", validator: ...) { ipld in ... }
} 

/// Somewhere else without reference to `dht`
app.dht.getValue(forKey: "Qm12...") { value in .... } //if you only have one DHT running
/// or
app.dhts.for(id: .MyDHT).put(value: Record, forKey: "Qm12...") { success in ... }
/// or
let val = await app.dhts.for(id: "MyDHT").findNode("Qm13...")
let routingTableInfo = await app.dhts.for(id: "MyDHT").routingTableInfo()

}

Contributing

Contributions are welcomed! This code is very much a proof of concept. I can guarantee you there's a better / safer way to accomplish the same results. Any suggestions, improvements, or even just critques, are welcome!

Let's make this code better together! 🤝

Credits

License

MIT © 2022 Breth Inc.

swift-libp2p-kad-dht's People

Contributors

btoms20 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

bevice1

swift-libp2p-kad-dht's Issues

Importing Build error

Hi, when i use the Swift Package Manager to import your Library. I get a build error "cannot find type 'ResponseType' in scope".
If I clone the repo and import it as a local library, there are no build errors but none of your examples work. Despite importing LibP2PKadDHT it cannot find classes like BasicKadDHT.

XCode Version 15.0 (15A240d)
iOS 17

How is your package supposed to be used in a project? I saw the installation guide but thats only for importing to another package.

Kind regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.