Skip to content
ebfull edited this page May 17, 2014 · 5 revisions

Developing with simbit

Github!

-Your daily reminder that github has yet to improve diff syntax highlighting.
+Your daily reminder that github is awesome.

Getting Started

First, clone the simbit repository with git clone https://github.com/ebfull/simbit.git and visit the index.html page to see if everything is working. Or, if you'd like, run node sim.js to simulate without a web browser.

Editing sim.js

Let's put the following into sim.js:

var net = require("./network")
var client = new net.Client()

net.add(100, client)
net.run(100 * 1000)

This is the bare minimum necessary. Refresh index.html and press play -- 100 nodes will be created, but they won't do anything.

The network module is the core of simbit. The client object is a template which is used to instantiate nodes later. 100 nodes are instantiated and the simulation is run for 100 seconds. (All time is in msec.)

peermgr

The peermgr module is a basic interface for discovering, connecting to and communicating with other nodes. It's intended to mimic the bitcoin peer manager and address manager. Adding it to your simulation is simple:

 var net = require("./network")
+var peermgr = require("./peermgr") // include peermgr
 var client = new net.Client()

+client.use(peermgr) // use the peermgr middleware
+
 net.add(100, client)
 net.run(100 * 1000)

Your simulation should now form a network. Learn more about what you can do with peermgr.

Initialization

To give our nodes custom behaviors, we use Client.init:

 client.use(peermgr) // use the peermgr middleware

+client.init(function() {
+       // this is a NodeState object unique to this node
+
+       this.log("I am node #" + this.id);
+})
+
 net.add(100, client)
 net.run(100 * 1000)

Notice the use of this.log() and this.id, both properties of the NodeState object.

Events

Tick events take place repeatedly until the tick function returns false.

client.init(function() {
        this.tick(1000, function() {
                // this will be called every 1 second
        })
})

We can also schedule rare events that occur probabilistically.

client.init(function() {
        this.prob("mining", 1/1000, function() {
                // this will be called, on average, every 1 second
                this.log("We mined a block!")
        })
})