<commit_before>var vows = require('vows'),
    assert = require('assert');

var util = require('util');
var events = require('events');

var Settings = require('../lib/settings').Settings;
var Node = require('../lib/node').Node;
var Peer = require('../lib/peer').Peer;
var Util = require('../lib/util');
var Step = require('step');
var encodeHex = Util.encodeHex;

vows.describe('Network Simulation').addBatch({
  'A simulated Bitcoin network': {
    topic: function () {
      var net = {};
      Step(
        function createServerStep() {
          var settings = getSettings('server');
          createNode(settings, this);
        },
        function indexServerStep(err, server) {
          if (err) throw err;
          net.server = server;
          this(null);
        },
        function createClientStep(err) {
          if (err) throw err;
          var settings = getSettings('client');
          createNode(settings, this);
        },
        function indexClientStep(err, client) {
          if (err) throw err;
          net.client = client;
          this(null);
        },
        function connectNodesStep(err) {
          if (err) throw err;
          var callback = this;
          connectNodes(net.server, net.client);
          net.client.getPeerManager().once('connect', function () {
            callback(null);
          });
        },
        function finalStep(err) {
          if (err) throw err;
          this(null, net);
        },
        this.callback
      );
      return;
    },
    'has a server node': function (topic) {
      assert.instanceOf(topic.server, Node);
    },
    'has a client node': function (topic) {
      assert.instanceOf(topic.client, Node);
    }
  }
}).export(module);

function getSettings(prefix) {
  var settings = new Settings();
  settings.setUnitnetDefaults();
  settings.storage.uri = 'leveldb:///tmp/'+prefix;
  settings.jsonrpc.enable = false;
  settings.network.noListen = true;
  return settings;
};

function createNode(settings, callback) {
  try {
    var node = new Node(settings);
    node.addListener('stateChange', function (e) {
      if (e.newState == 'netConnect') {
        callback(null, node);
      }
    });
    node.start();<commit_msg>Simnet: Ensure createNode only calls its callback once.<commit_after>var vows = require('vows'),
    assert = require('assert');

var util = require('util');
var events = require('events');

var Settings = require('../lib/settings').Settings;
var Node = require('../lib/node').Node;
var Peer = require('../lib/peer').Peer;
var Util = require('../lib/util');
var Step = require('step');
var encodeHex = Util.encodeHex;

vows.describe('Network Simulation').addBatch({
  'A simulated Bitcoin network': {
    topic: function () {
      var net = {};
      Step(
        function createServerStep() {
          var settings = getSettings('server');
          createNode(settings, this);
        },
        function indexServerStep(err, server) {
          if (err) throw err;
          net.server = server;
          this(null);
        },
        function createClientStep(err) {
          if (err) throw err;
          var settings = getSettings('client');
          createNode(settings, this);
        },
        function indexClientStep(err, client) {
          if (err) throw err;
          net.client = client;
          this(null);
        },
        function connectNodesStep(err) {
          if (err) throw err;
          var callback = this;
          connectNodes(net.server, net.client);
          net.client.getPeerManager().once('connect', function () {
            callback(null);
          });
        },
        function finalStep(err) {
          if (err) throw err;
          this(null, net);
        },
        this.callback
      );
      return;
    },
    'has a server node': function (topic) {
      assert.instanceOf(topic.server, Node);
    },
    'has a client node': function (topic) {
      assert.instanceOf(topic.client, Node);
    }
  }
}).export(module);

function getSettings(prefix) {
  var settings = new Settings();
  settings.setUnitnetDefaults();
  settings.storage.uri = 'leveldb:///tmp/'+prefix;
  settings.jsonrpc.enable = false;
  settings.network.noListen = true;
  return settings;
};

function createNode(settings, callback) {
  try {
    var node = new Node(settings);
    node.on('stateChange', function (e) {
      if (e.newState == 'netConnect') {
        node.removeListener('stateChange', arguments.callee);
        callback(null, node);
      }
    });
    node.start();
{'fraction_non_alphanumeric': 0.1430772617213295, 'fraction_numerical': 0.0, 'mean_word_length': 2.319211102994887, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6460784', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1273, 'n_words': 310}