Move bee movie demo to examples and cleanup

This commit is contained in:
Stuckinaboot 2019-05-08 19:48:21 -04:00
parent 7cc9ddda75
commit 4c2bf6873a
4 changed files with 6 additions and 39 deletions

View File

@ -10,19 +10,8 @@ from ordered_queue import OrderedQueue
SUPPORTED_PUBSUB_PROTOCOLS = ["/floodsub/1.0.0"] SUPPORTED_PUBSUB_PROTOCOLS = ["/floodsub/1.0.0"]
BEE_MOVIE_TOPIC = "bee_movie" BEE_MOVIE_TOPIC = "bee_movie"
# Message format:
# Sending crypto: <source>,<dest>,<amount as integer>
# Ex. send,aspyn,alex,5
# Set crypto: <dest>,<amount as integer>
# Ex. set,rob,5
# Determine message type by looking at first item before first comma
class MsgOrderingNode(): class MsgOrderingNode():
"""
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
"""
def __init__(self): def __init__(self):
self.balances = {} self.balances = {}
@ -71,12 +60,10 @@ class MsgOrderingNode():
asyncio.ensure_future(self.handle_incoming_msgs()) asyncio.ensure_future(self.handle_incoming_msgs())
async def publish_bee_movie_word(self, word, msg_id=None): async def publish_bee_movie_word(self, word, msg_id=None):
# print("Publish hit for " + word)
my_id = str(self.libp2p_node.get_id()) my_id = str(self.libp2p_node.get_id())
if msg_id is None: if msg_id is None:
msg_id = self.next_msg_id_func() msg_id = self.next_msg_id_func()
packet = generate_RPC_packet(my_id, [BEE_MOVIE_TOPIC], word, msg_id) packet = generate_RPC_packet(my_id, [BEE_MOVIE_TOPIC], word, msg_id)
# print("Packet generated")
await self.floodsub.publish(my_id, packet.SerializeToString()) await self.floodsub.publish(my_id, packet.SerializeToString())
async def handle_bee_movie_word(self, seqno, word): async def handle_bee_movie_word(self, seqno, word):
@ -87,9 +74,3 @@ class MsgOrderingNode():
# Get just the word (and not the seqno) and return the word # Get just the word (and not the seqno) and return the word
next_word = (await self.priority_queue.get())[1] next_word = (await self.priority_queue.get())[1]
return next_word return next_word
# if (self.priority_queue.qsize() > 0 and \
# self.priority_queue[0][0] == self.last_word_gotten_seqno + 1):
# return await self.priority_queue.get()
# else:
# # Wait until a word is put with next_word_seqno == self.last_word_gotten_seqno + 1
# # Then return first element of priority queue

View File

@ -12,13 +12,10 @@ class OrderedQueue():
self.task = None self.task = None
async def put(self, item): async def put(self, item):
# print("Put " + str(item))
seqno = item[0] seqno = item[0]
await self.queue.put(item) await self.queue.put(item)
# print("Added to queue " + str(item))
if self.last_gotten_seqno + 1 == seqno and self.task is not None: if self.last_gotten_seqno + 1 == seqno and self.task is not None:
# Allow get future to return the most recent item that is put # Allow get future to return the most recent item that is put
# print("Set called")
self.task.set() self.task.set()
async def get(self): async def get(self):
@ -26,30 +23,20 @@ class OrderedQueue():
front_item = await self.queue.get() front_item = await self.queue.get()
if front_item[0] == self.last_gotten_seqno + 1: if front_item[0] == self.last_gotten_seqno + 1:
# print("Trivial get " + str(front_item))
self.last_gotten_seqno += 1 self.last_gotten_seqno += 1
return front_item return front_item
else: else:
# Put element back as it should not be delivered yet # Put element back as it should not be delivered yet
# print("Front item put back 1")
# print(str(self.last_gotten_seqno))
# print(front_item[1])
await self.queue.put(front_item) await self.queue.put(front_item)
# print("Front item put back 2")
# print("get 2") # Wait until item with subsequent seqno is put on queue
# Wait until
self.task = asyncio.Event() self.task = asyncio.Event()
# print("get 3")
await self.task.wait() await self.task.wait()
# print("get 4")
item = await self.queue.get() item = await self.queue.get()
# print("get 5")
# print(str(item) + " got from queue")
# Remove task # Remove task
self.task = None self.task = None
self.last_gotten_seqno += 1 self.last_gotten_seqno += 1
return item return item

View File

@ -27,12 +27,11 @@ def create_setup_in_new_thread_func(dummy_node):
async def perform_test(num_nodes, adjacency_map, action_func, assertion_func): async def perform_test(num_nodes, adjacency_map, action_func, assertion_func):
""" """
Helper function to allow for easy construction of custom tests for dummy account nodes Helper function to allow for easy construction of custom tests for msg ordering nodes
in various network topologies in various network topologies
:param num_nodes: number of nodes in the test :param num_nodes: number of nodes in the test
:param adjacency_map: adjacency map defining each node and its list of neighbors :param adjacency_map: adjacency map defining each node and its list of neighbors
:param action_func: function to execute that includes actions by the nodes, :param action_func: function to execute that includes actions by the nodes
such as send crypto and set crypto
:param assertion_func: assertions for testing the results of the actions are correct :param assertion_func: assertions for testing the results of the actions are correct
""" """
@ -54,12 +53,12 @@ async def perform_test(num_nodes, adjacency_map, action_func, assertion_func):
# Start a thread for each node so that each node can listen and respond # Start a thread for each node so that each node can listen and respond
# to messages on its own thread, which will avoid waiting indefinitely # to messages on its own thread, which will avoid waiting indefinitely
# on the main thread. On this thread, call the setup func for the node, # on the main thread. On this thread, call the setup func for the node,
# which subscribes the node to the CRYPTO_TOPIC topic # which subscribes the node to the BEE_MOVIE_TOPIC topic
for dummy_node in dummy_nodes: for dummy_node in dummy_nodes:
thread = Thread(target=create_setup_in_new_thread_func(dummy_node)) thread = Thread(target=create_setup_in_new_thread_func(dummy_node))
thread.run() thread.run()
# Allow time for nodes to subscribe to CRYPTO_TOPIC topic # Allow time for nodes to subscribe to BEE_MOVIE_TOPIC topic
await asyncio.sleep(0.25) await asyncio.sleep(0.25)
# Perform action function # Perform action function