Hide this info
TL;DR: Quick start
1. Click the "Initialize nodes" button (scroll down)
2. Click anywhere in the grid that appears
3. You'll see the message spreading throughout the network
4. ???
5. Everyone has the message!
Note that nodes are randomly connected. A square on the left top can be directly connected to one on the right bottom.
Longer, more informative version
This page performs a very simple simulation of a mesh network. It is mostly modeled with the Bitcoin network in mind, but
any partially connected mesh network is the same. Bittorrent too, perhaps, except in Bittorrent the messages are not one byte
each and network speed is not measured in bytes per second.
If this is the first time you're here, I suggest leaving all settings and clicking the "Initialize nodes" button. This will
display a grid with green squares. Each of the squares represents a node (a user of the mesh network) and can be clicked.
When you click it, the node will start sending out messages to nodes that he is connected to. Those connected nodes will then
start sending it to nodes that they are connected to, and so on.
The goal of the experiment was to see how long it takes to spread a message (e.g. a transaction in the Bitcoin network) to
each node in the network (answer: surprisingly quick!). Or if there would be clustering of nodes, where you basically have
two or more independent clusters/networks of nodes that have no clue about each other's existence (answer: this doesn't seem
to happen).
If you want to go further and simulate a network with a million nodes, go ahead and enter a million in the "number of nodes"
field. Beware though that visualizing this can take forever, so you might want to untick "Visualize". You will only be shown
stats and will need to use the "Activate random node" and "Activate node #1" buttons to send messages.
If your browser warns that the script may have gotten stuck, press "Continue anyway" (or whatever it says) a couple of times.
It never got stuck for me, it just takes some time to calculate who's sending and receiving which message. A million nodes
took my laptop (i7 3rd gen) at least 30 seconds to spread the message throughout the network, unvisualized of course, but you
can still see how many frames it took and thus how much time it would take in the real world.
To reset the system state, you can always hit 'Initialize nodes' again (with or without modified properties on top of the
page). This will give you a new, clean version of the simulation. Also ticking or unticking Visualize will reset the system.
Info for developers
If you want to play with it a bit more and add some scripts, go ahead and use the developer console. For example, to send a
message originating at node 10, run: clicked(10). The object with all node 10's properties is at nodes[10]. Existing properties:
- array connected_nodes: pointers to connected nodes
- array messages: which messages the node has received
- bool is_supernode
- int id (its nodes[] index number, e.g. x=1;x==nodes[x].id)
- float speed: its sending capacity
- float currently_sending: how fast it is currently sending (not receiving)
- bool active
- bool rogue (currently unused)
Unofficially, you can also send messages by using:
nodes[10].messages.push("A"); nodes_active = 1;
This might mess things up, like the message spread will not be shown, but it works. It works because the page continuously checks
for each node whether each of its connected nodes has the messages as itself, and if not, pushes anything missing. The only reason
that merely pushing a message to the messages array doesn't work is because the page doesn't check for message synchronization
when there are no active nodes. This is merely an optimization, and setting nodes_active=1 works around that requirement. In fact,
running clicked(n) will also simply set nodes_active=1.
Other useful functions and globalvars:
(Note that you usually need to pass the node (e.g. nodes[n]), not its id number.)
- void connect(node1, node2) // Bidirectionally connect node1<->node2, if they weren't already
- undefined finished(...) // This does not exist! But you can fill it in and the code will use it*
- int running // The setInterval from the main loop. clearInterval(running)
- int supernodes_active // current number of supernodes actively tranmsitting
- DOM_object $(id) // returns document.getElementById(id)
- void sendmessage(tonode, msg) // pushes the message, sets nodes_active++ and calls activate_node
- void activate_node(node) // Visually activates a node
- void deactivate_node(node) // Visually deactivates a node
- void updateNetstats() // Updates the displayed statistics
- node getRandomNode()
* finished() will be called whenever there are no active nodes anymore. You can do things like:
t=current_time;dowhatever();finished=function(){alert(currenttime-t);}
Another example: you can create a full mesh network (each node being connected to every other node) with the following code:
for (var i in nodes) {
for (var j in nodes) {
connect(nodes[i], nodes[j]);
}
}
Contact
If you have useful (or just cool) uses for this project, tell me about it! I'm interested to see what people do with it!
Contact info is available at:
lucb1e.com/!about. Send me an email or tweet screenshots :)
Note to self: TODO: Build in network attack simulations (availability of the network (message spread speed from a random host to the
network) when 1 host is DoSing, or perhaps when a few nodes are DDoSing).