bbc1.core.bbc_network module

Copyright (c) 2017 beyond-blockchain.org.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

class bbc1.core.bbc_network.BBcNetwork(config, core=None, p2p_port=None, external_ip4addr=None, external_ip6addr=None, loglevel='all', logname=None)[source]

Bases: object

Socket and thread management for infrastructure layers

CONFIRM_KEY_EXCHANGE = b'\x00\x03'
NOTIFY_LEAVE = b'\x00\x00'
REQUEST_KEY_EXCHANGE = b'\x00\x01'
RESPONSE_KEY_EXCHANGE = b'\x00\x02'
add_neighbor(domain_id, node_id, ipv4=None, ipv6=None, port=None, is_static=False)[source]

Add node in the neighbor list

Parameters:
  • domain_id (bytes) – target domain_id
  • node_id (bytes) – target node_id
  • ipv4 (str) – IPv4 address of the node
  • ipv6 (str) – IPv6 address of the node
  • port (int) – Port number that the node is waiting at
  • is_static (bool) – If true, the entry is treated as static one and will be saved in config.json
Returns:

True if it is a new entry, None if error.

Return type:

bool

broadcast_message_in_network(domain_id, payload_type=1, msg=None)[source]

Send message to all neighbor nodes

Parameters:
  • payload_type (bytes) – message format type
  • domain_id (bytes) – target domain_id
  • msg (dict) – message to send
Returns:

True if successful

Return type:

bool

check_admin_signature(domain_id, msg)[source]

Check admin signature in the message

Parameters:
  • domain_id (bytes) – target domain_id
  • msg (dict) – received message
Returns:

True if valid

Return type:

bool

create_domain(domain_id=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', config=None)[source]

Create domain and register user in the domain

Parameters:
  • domain_id (bytes) – target domain_id to create
  • config (dict) – configuration for the domain
Returns:

Return type:

bool

get_domain_keypair(domain_id)[source]

Get domain_keys (private key and public key)

Parameters:domain_id (bytes) – target domain_id
include_admin_info_into_message_if_needed(domain_id, msg, admin_info)[source]

Serialize admin info into one binary object and add signature

remove_domain(domain_id=b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')[source]

Leave the domain and remove it

Parameters:domain_id (bytes) – target domain_id to remove
Returns:True if successful
Return type:bool
save_all_static_node_list()[source]

Save all static nodes in the config file

send_domain_ping(domain_id, ipv4, ipv6, port, is_static=False)[source]

Send domain ping to the specified node

Parameters:
  • domain_id (bytes) – target domain_id
  • ipv4 (str) – IPv4 address of the node
  • ipv6 (str) – IPv6 address of the node
  • port (int) – Port number
  • is_static (bool) – If true, the entry is treated as static one and will be saved in config.json
Returns:

True if successful

Return type:

bool

send_key_exchange_message(domain_id, node_id, command, pubkey, nonce, random_val, key_name)[source]

Send ECDH key exchange message

send_message_in_network(nodeinfo=None, payload_type=1, domain_id=None, msg=None)[source]

Send message over a domain network

Parameters:
  • nodeinfo (NodeInfo) – NodeInfo object of the destination
  • payload_type (bytes) – message format type
  • domain_id (bytes) – target domain_id
  • msg (dict) – message to send
Returns:

True if successful

Return type:

bool

send_message_to_a_domain0_manager(domain_id, msg)[source]

Choose one of domain0_managers and send msg to it

Parameters:
  • domain_id (bytes) – target domain_id
  • msg (bytes) – message to send
setup_tcp_server()[source]

Start tcp server

setup_udp_socket()[source]

Setup UDP socket

tcpserver_loop()[source]

Message loop for TCP socket

udp_message_loop()[source]

Message loop for UDP socket

class bbc1.core.bbc_network.NeighborInfo(network=None, domain_id=None, node_id=None, my_info=None)[source]

Bases: object

Manage information of neighbor nodes

NODEINFO_LIFETIME = 900
PURGE_INTERVAL_SEC = 300
add(node_id, ipv4=None, ipv6=None, port=None, is_static=False, domain0=None)[source]

Add or update an neighbor node entry

purge(query_entry)[source]

Purge obsoleted entry in nodeinfo_list

remove(node_id)[source]

Remove entry in the nodeinfo_list

show_list()[source]

Return nodeinfo list in human readable format

class bbc1.core.bbc_network.NodeInfo(node_id=None, ipv4=None, ipv6=None, port=None, is_static=False, domain0=False)[source]

Bases: object

Node information entry

SECURITY_STATE_CONFIRMING = 2
SECURITY_STATE_ESTABLISHED = 3
SECURITY_STATE_NONE = 0
SECURITY_STATE_REQUESTING = 1
get_nodeinfo()[source]

Return a list of node info

Returns:[node_id, ipv4, ipv6, port, domain0_flag, update_at]
Return type:list
touch()[source]
update(ipv4=None, ipv6=None, port=None, seq=None, domain0=None)[source]

Update the entry

Parameters:
  • ipv4 (str) – IPv4 address of the sender node
  • ipv6 (str) – IPv6 address of the sender node
  • port (int) – Port number of the sender
  • sec (int) – message sequence number
  • domain0 (bool or None) – If True, the node is domain0 manager
Returns:

True if the entry has changed

Return type:

bool

bbc1.core.bbc_network.is_less_than(val_a, val_b)[source]

Return True if val_a is less than val_b (evaluate as integer)