libp2p.tools.pubsub package

Submodules

libp2p.tools.pubsub.dummy_account_node module

class libp2p.tools.pubsub.dummy_account_node.DummyAccountNode(pubsub: Pubsub)

Bases: Service

Node which has an internal balance mapping, meant to serve as a dummy crypto blockchain.

There is no actual blockchain, just a simple map indicating how much crypto each user in the mappings holds

classmethod create(number: int) AsyncIterator[Tuple[DummyAccountNode, ...]]

Create a new DummyAccountNode and attach a libp2p node, a floodsub, and a pubsub instance to this new node.

We use create as this serves as a factory function and allows us to use async await, unlike the init function

get_balance(user: str) int

Get balance in crypto for a particular user.

Parameters:

user – user to get balance for

Returns:

balance of user

async handle_incoming_msgs() None

Handle all incoming messages on the CRYPTO_TOPIC from peers.

handle_send_crypto(source_user: str, dest_user: str, amount: int) None

Handle incoming send_crypto message.

Parameters:
  • source_user – user to send crypto from

  • dest_user – user to send crypto to

  • amount – amount of crypto to send

handle_set_crypto(dest_user: str, amount: int) None

Handle incoming set_crypto message.

Parameters:
  • dest_user – user to set crypto for

  • amount – amount of crypto

property host: IHost
async publish_send_crypto(source_user: str, dest_user: str, amount: int) None

Create a send crypto message and publish that message to all other nodes.

Parameters:
  • source_user – user to send crypto from

  • dest_user – user to send crypto to

  • amount – amount of crypto to send

async publish_set_crypto(user: str, amount: int) None

Create a set crypto message and publish that message to all other nodes.

Parameters:
  • user – user to set crypto for

  • amount – amount of crypto

pubsub: Pubsub
async run() None

Primary entry point for all service logic.

Note

This method should not be directly invoked by user code.

Services may be run using the following approaches.

libp2p.tools.pubsub.floodsub_integration_test_settings module

async libp2p.tools.pubsub.floodsub_integration_test_settings.perform_test_from_obj(obj, pubsub_factory) None

Perform pubsub tests from a test object, which is composed as follows:

{
    "supported_protocols": ["supported/protocol/1.0.0",...],
    "adj_list": {
        "node1": ["neighbor1_of_node1", "neighbor2_of_node1", ...],
        "node2": ["neighbor1_of_node2", "neighbor2_of_node2", ...],
        ...
    },
    "topic_map": {
        "topic1": ["node1_subscribed_to_topic1", "node2_subscribed_to_topic1", ...]
    },
    "messages": [
        {
            "topics": ["topic1_for_message", "topic2_for_message", ...],
            "data": b"some contents of the message (newlines are not supported)",
            "node_id": "message sender node id"
        },
        ...
    ]
}

Note

In adj_list, for any neighbors A and B, only list B as a neighbor of A or B as a neighbor of A once. Do NOT list both A: [“B”] and B:[“A”] as the behavior is undefined (even if it may work)

libp2p.tools.pubsub.utils module

async libp2p.tools.pubsub.utils.connect_some(hosts: Sequence[IHost], degree: int) None
async libp2p.tools.pubsub.utils.dense_connect(hosts: Sequence[IHost]) None
libp2p.tools.pubsub.utils.make_pubsub_msg(origin_id: ID, topic_ids: Sequence[str], data: bytes, seqno: bytes) Message
async libp2p.tools.pubsub.utils.one_to_all_connect(hosts: Sequence[IHost], central_host_index: int) None

Module contents