2.2.12 Cycle 12 - Sending messages across the network
Design
Objectives
Since the project is getting near the end of the time period allowed for development and the validation/transaction part of the blockchain doesn't exist outside of the theoretical space yet, but the majority communication layer is nearly finished I think it would be a good idea to get general communication working such that by the end of the allowed period of development the project exists in a form that is somewhat usable.
This form would effectively end up being a decentralised messaging service, which although is not quite the full blockchain that I was aiming for in the first place, still utilises a lot of the same networking and logic - due to effectively being a demo of the decentralised communication layer.
To do this, I will need to introduce a few new features that will build upon what I have created in previous cycles:
With these features the messages being sent will be very similar to the transactions that were initially hypothesised in the analysis of this project, however instead of being sent in bulk during block creation, will instead be sent individually whenever nodes feel like it.
This will then allow for the adaption of these messages into blocks in a future cycle if there is enough time to continue on this path, meaning that in any form this is not a detour of the theorised final project, just a way to create a demo that uses everything that has been created so far.
Usability Features
Receiving messages - Only store valid messages so as to prevent the waste of storage on a user's computer that comes with storing invalid messages.
Logging - Continuous logging should be made while the program operates so that the user can check what is going on with their node by checking the terminal output/logs.
Key Variables
This object holds a message received or created for broadcasting, it holds all necessary data such as the sender, timestamp, contents, etc. It's useful for ensuring that messages broadcasted across the message are all consistently typed.
This object wraps the Broadcast_Message_Contents object and contains a signature that confirms that the sender did in fact send and approve a message.
Pseudocode
The API endpoint
This is the endpoint at which other nodes will send their own messages to be broadcasted onwards, although it also includes some logic to ensure that the messages received are indeed valid before the node will send them onwards.
Functionality for internal use
These are the functions that will allow the node to handle sending messages internally and these functions will be called by other sections - such as the dashboard page also being developed in this cycle.
Development
An addition that I ended up creating for this cycle that wasn't originally planned but was very useful to test and use the functionality created was a basic html page that was hosted on the http server to allow a user to send and view messages in an easy to use, simple web page.
Paired with all the new broadcasting functionality that allows messages to be shared across the network and the development for this cycle ends up being a lot more substantial than the pseudocode would make it appear, hence the outcome section simply includes two relevant files in their entirety as they were both created during this cycle and both contain relevant code.
The first being the broadcast file that shows all the code written to send messages across the network and forward those that have been received and checked to be valid.
Then the second being the user sided dashboard that helps to provide a nice way to view and contribute to these messages.
Outcome
The broadcast file
This file is split up into three key sections: the setup; the api endpoints; and the internal functions; this is because although it would be briefer to just show the api endpoints and the internal functions, the setup helps to give more context to those two sections so I included it anyway.
The dashboard file
This file houses all the logic and code for the http dashboard that is hosted the same way that the api endpoints are, with a basic token system to allow a node's 'owner' to send messages from anywhere that can access the node's api server
Http endpoints
Both of these endpoints reference http pages to be hosted by the api server, I have decided not to include the html files because they are both quite a lot of lines due to housing html, javascript and css code all within the same file, but the files can be found here.
Api endpoints
All of the endpoints in this section are accessed by html requests within code and are not meant to be accessed directly by users, and handle the token logic to provide a somewhat secure method of allowing a node's owner to talk to the node over the internet. This is not the most secure way this could've been done but should be fine by now.
Challenges
One of the key challenges this cycle was to figure out how to give access to the dashboard to the node's owner and not to other users without having to make them send any private data over a network as it is fairly probable that the software will be running without https as it isn't required for any other part of the software.
The method I created to handle this is very simple but should be secure enough for now and simply relies upon the user trying to login having access to the terminal output of the software at login time.
This is because the security method is based upon a basic cookie and token system where the user clicks "get a token" to generate a new token that is then outputted in the software's terminal output, they can then copy this token into the input field to login where it is submitted as a form of password. Assuming this token is correct, the user's browser is then sent a cookie holding this token so that any other request sent by the user contains this token and verifies that they have control of this node.
Testing
Tests
1
Navigate to 'https://localhost:8000/dashboard'
The dashboard page should redirect to the login page with an option to input a token or generate one.
As Expected
2
Click the 'generate token' button
A token to be output in the node's terminal output.
As Expected
3
Enter an invalid token into the input field and click 'submit'
The dashboard should not give the user access.
As Expected
4
Enter an valid token into the input field and click 'submit'
The dashboard should give the user acess to itself.
As Expected
5
Type a message and click send.
The node should receive the message from the dashboard and attempt to send it across the network.
As Expected
6
Connect to and then send a message to another node using the dashboard.
The node should connect to a pre-existing node and then send a message to it after a message is sent from the dashboard.
As Expected
7
Send a message to the node from another node.
The node on the receipient end of test 6 should receive the same message as was sent from the other node.
As Expected
Last updated