Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChainNode sketch #2

Closed
emilbayes opened this issue Jul 31, 2019 · 0 comments
Closed

ChainNode sketch #2

emilbayes opened this issue Jul 31, 2019 · 0 comments
Assignees

Comments

@emilbayes
Copy link
Member

Here's a sketch of a "Chain-only" Node:

const { Chain, Node, Fees, Mempool, Pool } = require('bcoin')
const assert = require('nanoassert')

class ChainNode extends Node {
  constructor (options) {
    super('bcoin', 'bcoin.conf', 'debug.log', options)
    this.opened = false
    // SPV flag.
    this.spv = false
    // Instantiate blockchain.
    this.chain = new Chain({
      network: this.network,
      logger: this.logger,
      workers: this.workers,
      memory: this.config.bool('memory'),
      prefix: this.config.prefix,
      maxFiles: this.config.uint('max-files'),
      cacheSize: this.config.mb('cache-size'),
      forceFlags: this.config.bool('force-flags'),
      bip91: this.config.bool('bip91'),
      bip148: this.config.bool('bip148'),
      prune: this.config.bool('prune'),
      checkpoints: this.config.bool('checkpoints'),
      coinCache: this.config.mb('coin-cache'),
      entryCache: this.config.uint('entry-cache'),
      indexTX: this.config.bool('index-tx'),
      indexAddress: this.config.bool('index-address')
    })

    this.fees = new Fees(this.logger)
    this.fees.init()

    this.mempool = new Mempool({
      network: this.network,
      logger: this.logger,
      workers: this.workers,
      chain: this.chain,
      fees: this.fees,
      memory: this.config.bool('memory'),
      prefix: this.config.prefix,
      persistent: this.config.bool('persistent-mempool'),
      maxSize: this.config.mb('mempool-size'),
      limitFree: this.config.bool('limit-free'),
      limitFreeRelay: this.config.uint('limit-free-relay'),
      requireStandard: this.config.bool('require-standard'),
      rejectAbsurdFees: this.config.bool('reject-absurd-fees'),
      replaceByFee: this.config.bool('replace-by-fee'),
      indexAddress: this.config.bool('index-address')
    })

    this.pool = new Pool({
      network: this.network,
      logger: this.logger,
      chain: this.chain,
      mempool: this.mempool,
      prefix: this.config.prefix,
      selfish: this.config.bool('selfish'),
      compact: this.config.bool('compact'),
      bip37: this.config.bool('bip37'),
      bip151: this.config.bool('bip151'),
      bip150: this.config.bool('bip150'),
      identityKey: this.config.buf('identity-key'),
      maxOutbound: this.config.uint('max-outbound'),
      maxInbound: this.config.uint('max-inbound'),
      createSocket: this.config.func('create-socket'),
      proxy: this.config.str('proxy'),
      onion: this.config.bool('onion'),
      upnp: this.config.bool('upnp'),
      seeds: this.config.array('seeds'),
      nodes: this.config.array('nodes'),
      only: this.config.array('only'),
      publicHost: this.config.str('public-host'),
      publicPort: this.config.uint('public-port'),
      host: this.config.str('host'),
      port: this.config.uint('port'),
      listen: this.config.bool('listen'),
      memory: this.config.bool('memory')
    })

    this.init()
  }

  init () {
    // Bind to errors
    this.chain.on('error', err => this.error(err))
    this.mempool.on('error', err => this.error(err))
    this.pool.on('error', err => this.error(err))
    this.mempool.on('tx', (tx) => {
      this.emit('tx', tx)
    })
    this.chain.on('connect', async (entry, block) => {
      try {
        await this.mempool._addBlock(entry, block.txs)
      } catch (e) {
        this.error(e)
      }
      this.emit('block', block)
      this.emit('connect', entry, block)
    })
    this.chain.on('disconnect', async (entry, block) => {
      try {
        await this.mempool._removeBlock(entry, block.txs)
      } catch (e) {
        this.error(e)
      }
      this.emit('disconnect', entry, block)
    })
    this.chain.on('reorganize', async (tip, competitor) => {
      try {
        await this.mempool._handleReorg()
      } catch (e) {
        this.error(e)
      }
      this.emit('reorganize', tip, competitor)
    })
    this.chain.on('reset', async (tip) => {
      try {
        await this.mempool._reset()
      } catch (e) {
        this.error(e)
      }
      this.emit('reset', tip)
    })
  }

  async open () {
    assert(!this.opened, 'FullNode is already open.')
    this.opened = true
    await this.handlePreopen()
    await this.chain.open()
    await this.mempool.open()
    await this.pool.open()
    await this.handleOpen()
    this.logger.info('Node is loaded.')
  }

  scan (start, filter, iter) {
    return this.chain.scan(start, filter, iter)
  }

  connect () {
    return this.pool.connect()
  }

  startSync () {
    return this.pool.startSync()
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants