Skip to content

Latest commit

 

History

History
205 lines (163 loc) · 6.18 KB

router.md

File metadata and controls

205 lines (163 loc) · 6.18 KB

Router

The router is one of the most important parts of the network, its role is to structure the peer-to-peer network.

Structuring the network to make it more reliable by allowing nodes to stay interconnected directly or indirectly even in the event of one or more nodes going offline.

Contents

Constructor

new Router(options)

  • options:
    • contact: Contact The node's local contact informations.
    • rpc: RPC The remote procedure call class of the node.
    • capacity: Integer The maximum amount of buckets to be stored in the router.
    • peers: Integer The maximum amount of contacts to be stored per bucket.


Creates a new Router instance.

const plexus = require("plexus");

const contact = new plexus.Contact({host: "127.0.0.1", port: 8080});

const RPC = new RPC({
    port: 8080
});

//  Router creation
const router = new plexus.Router({
    contact: contact,
    rpc: RPC,
    capacity: 160,
    peers: 20
});

Getters

router.size


Returns the number of known contacts.

//  Get the number of contacts
const size = router.size;

router.contacts


Returns the list of every known contacts.

//  Get the list of contacts
const contacts = router.contacts;

Methods

router.distance(buffer0, buffer1)

  • buffer0: Buffer Buffer to calculate the XOR distance to.
  • buffer1: Buffer Buffer to calculate the XOR distance from.


Returns the XOR Metric distance between two buffers (buffers dont need to be of same length).

const distance = router.distance(Buffer.from(contact0.id), Buffer.from(contact1.id));

router.get_bucket_index(contact0, contact1)

  • contact0: Contact Contact to calculate the XOR distance to.
  • contact1: Contact Contact to calculate the XOR distance from.


Returns the bucket index of a contact based on its distance to another contact (generally the node's local contact).

const bucket_index = this.get_bucket_index(node.self, contact);

router.update_contact(contact)

  • contact: Contact Contact to add, remove or update with a newer verion.


Updates the list of contacts (add new, update order, replace old).

const contact = new plexus.Contact({
    host: host,
    port: port,
    id: id,
    clock: new plexus.VectorClock()
});

//  Add the contact if new, update if already known or ignore if not valid
router.update_contact(contact);

router.to_head(contact, bucket)

  • contact: Contact Contact to move to the head of the bucket.
  • bucket: Bucket Bucket in which to move the contact.


Moves the contact to the head of the specified bucket.

const contact = new plexus.Contact({
    host: host,
    port: port,
    id: id,
    clock: new plexus.VectorClock()
});

const bucket = new plexus.Bucket();

//  Move to head
router.to_head(contact, bucket)

router.to_tail(contact, bucket)

  • contact: Contact Contact to move to the tail of the bucket.
  • bucket: Bucket Bucket in which to move the contact.


Moves the contact to the tail of the specified bucket.

const contact = new plexus.Contact({
    host: host,
    port: port,
    id: id,
    clock: new plexus.VectorClock()
});

const bucket = new plexus.Bucket();

//  Move to tail
router.to_tail(contact, bucket)

router.ping_head(contact, bucket)

  • contact: Contact Contact to replace the head of the bucket with if it is unreachable.
  • bucket: Bucket Bucket in which to move the contact.


Replaces the contact at head of the specified bucket with the new contact if it does not respond (no longer online or unreachable).

const contact = new plexus.Contact({
    host: host,
    port: port,
    id: id,
    clock: new plexus.VectorClock()
});

const bucket = new plexus.Bucket();

//  Replace if head is unreachable
router.ping_head(contact, bucket)

router.get_contacts_near(buffer, limit, sender)

  • buffer: Buffer Buffer to calculate the XOR distance from.
  • limit: Integer The maximum number of contacts to return.
  • sender: Buffer Buffer to calculate the XOR distance to.


Returns a list of the nearest contacts from the specified buffer.

//  The local node is 0xa45681de963cbfb2e841d2c94d450312347x3b235fb70fb27f2f69285ce481ce
//  We want a list of the 20 closest known contacts to 0xa6564bce963cbfb2e841d2c94d450368c1463b235fb70fb27f2f69285cacf8ed
const contacts = router.get_contacts_near(Buffer.from("0xa6564bce963cbfb2e841d2c94d450368c1463b235fb70fb27f2f69285cacf8ed"), 20, Buffer.from("0xa45681de963cbfb2e841d2c94d450312347x3b235fb70fb27f2f69285ce481ce"));

router.has_contact_id(id)

  • id: String The ID of the contact to look for.


Checks if the router already contains a contact with the specified ID.

//  Returns true if the router contain a contact with that ID
const exists = router.has_contact_id("0xa6564bce963cbfb2e841d2c94d450368c1463b235fb70fb27f2f69285cacf8ed");

router.get_contact(id)

  • id: String The ID of the contact to look for.


Returns a contact with the specified ID if the router contains one.

//  If contact is null the router doesn't contain any contacts with that ID
const contact = router.get_contact("0xa6564bce963cbfb2e841d2c94d450368c1463b235fb70fb27f2f69285cacf8ed");