Lightclient implementation library for witnet node
Project description
Witnet Library
This library is light client implementation for witnet node, capable of directly interacting with witnet node. ( See Witnet.io for more information )
Features
- Allows sending proto messages to witnet node.
- Performs handshake with witnet node.
- Iterates over the nodes in witnet network in a DAG(directed acyclic graph) fashion
How to use
Performing handshake with witnet node.
from witnet_lib.witnet_client import WitnetClient
from witnet_lib.utils import AttrDict
# Setting config
config = AttrDict()
config.update({
"genesis_sec": 1592996400,
"magic": 36162,
"sender_addr": "127.0.0.1:21341",
"time_per_epoch": 45,
})
client = WitnetClient(config)
client.handshake("127.0.0.1:21337")
client.close()
Listening to messages from witnet node.
#After performing handshake with node.
msg = client.tcp_handler.receive_witnet_msg() # this returns serialized message from node
parsed_msg = client.msg_handler.parse_msg(msg) # we need to parse the message
print(parsed_msg)
print(parsed_msg.kind)
The connection is of keep alive
type, so messages are continually sent from witnet node. To listen for all messages:
while True:
msg = client.tcp_handler.receive_witnet_msg() # this returns serialized message from node
parsed_msg = client.msg_handler.parse_msg(msg)
print(parsed_msg)
Sending message to witnet node.
#After performing handshake with node.
cmd = client.msg_handler.version_cmd("127.0.0.1:21337") # this returns a version message
print(cmd)
msg = client.msg_handler.serialize(cmd)# this returns serialized messsage ready to be sent to node
client.tcp_handler.send(msg)
msg_from_node = client.tcp_handler.receive_witnet_msg() # this returns only one whole message from node
msg_from_node_with_msg_len = client.tcp_handler.receive(30) # this returns x bytes from connection stream
Mapping all nodes in the network (DAG fashion)
from witnet_lib import utils
from witnet_lib.map_nodes import MapNodes
config = utils.AttrDict()
config.update({
"genesis_sec": 1592996400,
"magic": 36162,
"sender_addr": "127.0.0.1:21341",
"time_per_epoch": 45,
})
mapper = MapNodes(config, ["127.0.0.1:21337"]) # provide initialisation peers
all_nodes = mapper.start_mapping_workers(3) # number of connections allowed to be created in parallel
print(all_nodes)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
witnet_lib-0.0.1.tar.gz
(16.9 kB
view hashes)
Built Distribution
witnet_lib-0.0.1-py3-none-any.whl
(18.0 kB
view hashes)
Close
Hashes for witnet_lib-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcd41d499f4c68e044c41c6f809ef2e6773ff7d24362c4ce38482063968ef4d9 |
|
MD5 | e3780559e110a0e8bc2ebad4cf42967e |
|
BLAKE2b-256 | 1cce13b4a9cedfad6a178cad0869bc373cf8d261cd79e7a52c4882676ddfc943 |