#!/usr/bin/env node

'use strict';

process.title = 'bcoin';

const assert = require('assert');
const SPVNode = require('../lib/node/spvnode');
const util = require('../lib/utils/util');
const Outpoint = require('../lib/primitives/outpoint');

const node = SPVNode({
  config: true,
  argv: true,
  env: true,
  logFile: true,
  logConsole: true,
  logLevel: 'debug',
  db: 'leveldb',
  persistent: true,
  workers: true,
  listen: true,
  loader: require
});

// Temporary hack
if (!node.has('walletdb')) {
  const plugin = require('../lib/wallet/plugin');
  node.use(plugin);
}

process.on('unhandledRejection', (err, promise) => {
  throw err;
});

(async () => {
  await node.ensure();
  await node.open();
  await node.connect();

  if (node.config.bool('test')) {
    node.pool.watchAddress('1VayNert3x1KzbpzMGt2qdqrAThiRovi8');
    node.pool.watchOutpoint(new Outpoint());
    node.on('block', (block) => {
      assert(block.txs.length >= 1);
      if (block.txs.length > 1)
        util.log(block.txs[1]);
    });
  }

  node.startSync();
})().catch((err) => {
  console.error(err.stack);
  process.exit(1);
});
