Skip to content

Commit

Permalink
add networkName to opts
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Mar 1, 2015
1 parent 7573e99 commit 2cd0f92
Show file tree
Hide file tree
Showing 21 changed files with 487 additions and 477 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
"README.md"
],
"dependencies": {
"bigi": "^1.4.0",
"bigi": "1.4.0",
"browser-request": "0.3.3",
"bs58check": "1.0.4",
"errno": "0.1.1",
"eventemitter2": "0.4.14",
"lodash": "3.0.0",
"lru-cache": "2.5.0",
"q": "1.1.2",
"q": "1.2.0",
"request": "2.51.0",
"socket.io-client": "1.3.2",
"ws": "0.7.0",
Expand Down
37 changes: 25 additions & 12 deletions src/blockchain/blockchain.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var events = require('events')
var _ = require('lodash')
var EventEmitter = require('eventemitter2').EventEmitter2
var inherits = require('util').inherits

var Q = require('q')

var NotImplementedError = require('../errors').NotImplementedError
Expand All @@ -26,20 +26,33 @@ var yatc = require('../yatc')
/**
* @class Blockchain
* @extends events.EventEmitter
*
* @param {Network} network
* @param {Object} [opts]
* @param {string} [opts.networkName=bitcoin]
*/
function Blockchain(network) {
function Blockchain(network, opts) {
opts = _.extend({networkName: 'bitcoin'}, opts)
yatc.verify('Network', network)
yatc.verify('{networkName: String, ...}', opts)

events.EventEmitter.call(this)
EventEmitter.call(this)

Object.defineProperty(this, 'network', {enumerable: true, value: network})
this._networkName = opts.networkName

this._currentHeight = -1
this._currentBlockHash = util.zfill('', 64)
}

inherits(Blockchain, events.EventEmitter)
inherits(Blockchain, EventEmitter)

/**
* @return {string}
*/
Blockchain.prototype.getNetworkName = function () {
return this._networkName.slice()
}

/**
* @abstract
Expand All @@ -60,7 +73,7 @@ Blockchain.prototype.getCurrentBlockHash = function () {
/**
* @abstract
* @param {number} height
* @return {Q.Promise<BitcoinHeader>}
* @return {Promise<BitcoinHeader>}
*/
Blockchain.prototype.getHeader = function () {
return Q.reject(new NotImplementedError('Blockchain.getHeader'))
Expand All @@ -69,16 +82,16 @@ Blockchain.prototype.getHeader = function () {
/**
* @abstract
* @param {string} txId
* @return {Q.Promise<string>}
* @return {Promise<string>}
*/
Blockchain.prototype.getTx = function () {
return Q.reject(new NotImplementedError('Blockchain.getTx'))
}

/**
* @abstract
* @param {Transaction} tx
* @return {Q.Promise<string>}
* @param {string} tx
* @return {Promise<string>}
*/
Blockchain.prototype.sendTx = function () {
return Q.reject(new NotImplementedError('Blockchain.sendTx'))
Expand All @@ -87,7 +100,7 @@ Blockchain.prototype.sendTx = function () {
/**
* @abstract
* @param {string} address
* @return {Q.Promise<Network~HistoryObject>}
* @return {Promise<Network~HistoryObject>}
*/
Blockchain.prototype.getHistory = function () {
return Q.reject(new NotImplementedError('Blockchain.getHistory'))
Expand All @@ -96,7 +109,7 @@ Blockchain.prototype.getHistory = function () {
/**
* @abstract
* @param {string} address
* @return {Q.Promise<Network~UnspentObject>}
* @return {Promise<Network~UnspentObject>}
*/
Blockchain.prototype.getUnspent = function () {
return Q.reject(new NotImplementedError('Blockchain.getUnspent'))
Expand All @@ -105,7 +118,7 @@ Blockchain.prototype.getUnspent = function () {
/**
* @abstract
* @param {string} address
* @return {Q.Promise}
* @return {Promise}
*/
Blockchain.prototype.subscribeAddress = function () {
return Q.reject(new NotImplementedError('Blockchain.subscribeAddress'))
Expand Down
44 changes: 21 additions & 23 deletions src/blockchain/naive.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ var yatc = require('../yatc')
/**
* @class Naive
* @extends Blockchain
*
* @param {Network} network
* @param {Object} [opts]
* @param {string} [opts.networkName=bitcoin]
* @param {number} [opts.headerCacheSize=50]
* @param {number} [opts.txCacheSize=100]
*/
function Naive(network, opts) {
opts = _.extend({headerCacheSize: 50, txCacheSize: 100}, opts)
var self = this
Blockchain.call(self, network, opts)

opts = _.extend({headerCacheSize: 50, txCacheSize: 100}, opts)
yatc.verify('Network', network)
yatc.verify('{headerCacheSize: Number, txCacheSize: Number}', opts)

var self = this
Blockchain.call(self, network)
if (network.getNetworkName() !== self.getNetworkName()) {
throw new TypeError('Network and Blockchain have different networks')
}
yatc.verify('{headerCacheSize: Number, txCacheSize: Number, ...}', opts)

self._headerCache = LRU({max: opts.headerCacheSize})
self._txCache = LRU({max: opts.txCacheSize})
Expand All @@ -42,9 +46,8 @@ function Naive(network, opts) {
inherits(Naive, Blockchain)

/**
* @memberof Naive.prototype
* @method getHeader
* @see {@link Blockchain#getHeader}
* @param {number} height
* @return {Promise<BitcoinHeader>}
*/
Naive.prototype.getHeader = function (height) {
var self = this
Expand All @@ -62,9 +65,8 @@ Naive.prototype.getHeader = function (height) {
}

/**
* @memberof Naive.prototype
* @method getTx
* @see {@link Blockchain#getTx}
* @param {string} txId
* @return {Promise<string>}
*/
Naive.prototype.getTx = function (txId) {
var self = this
Expand All @@ -82,36 +84,32 @@ Naive.prototype.getTx = function (txId) {
}

/**
* @memberof Naive.prototype
* @method sendTx
* @see {@link Blockchain#sendTx}
* @param {string} txHex
* @return {Promise<string>}
*/
Naive.prototype.sendTx = function (txHex) {
return this.network.sendTx(txHex)
}

/**
* @memberof Naive.prototype
* @method getHistory
* @see {@link Blockchain#getHistory}
* @param {string} address
* @return {Promise<Network~HistoryObject>}
*/
Naive.prototype.getHistory = function (address) {
return this.network.getHistory(address)
}

/**
* @memberof Naive.prototype
* @method getUnspent
* @see {@link Blockchain#getUnspent}
* @param {string} address
* @return {Promise<Network~UnspentObject>}
*/
Naive.prototype.getUnspent = function (address) {
return this.network.getUnspent(address)
}

/**
* @memberof Naive.prototype
* @method subscribeAddress
* @see {@link Blockchain#subscribeAddress}
* @param {string} address
* @return {Promise}
*/
Naive.prototype.subscribeAddress = function (address) {
return this.network.subscribeAddress(address)
Expand Down
62 changes: 33 additions & 29 deletions src/blockchain/verified.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
var _ = require('lodash')
var assert = require('assert')
var inherits = require('util').inherits
var timers = require('timers')

var BigInteger = require('bigi')
var _ = require('lodash')
var inherits = require('util').inherits
var LRU = require('lru-cache')
var Q = require('q')
var timers = require('timers')

var Blockchain = require('./blockchain')
var errors = require('../errors')
Expand All @@ -23,7 +22,7 @@ var MAX_TARGET_BI = BigInteger.fromHex(MAX_TARGET)
* @param {number} index
* @param {string[]} headersChain
* @param {function} getHeader
* @return {Q.Promise<{bits: number, target: string}>}
* @return {Promise<{bits: number, target: string}>}
*/
function getTarget(index, headersChain, getHeader) {
if (index === 0) {
Expand Down Expand Up @@ -158,44 +157,49 @@ function verifyHeader(currentHash, currentHeader, prevHash, prevHeader, target,
* @param {Network} network
* @param {Object} opts
* @param {Storage} opts.storage
* @param {boolean} [opts.isTestnet=false]
* @param {boolean} [opts.compactMode=false]
* @param {string} [opts.networkName=bitcoin]
* @param {boolean} [opts.testnet=false]
* @param {boolean} [opts.useCompactMode=false]
* @param {boolean} [opts.usePreSavedChunkHashes=false]
* @param {number} [opts.headerCacheSize=10000] ~1.5MB
* @param {number} [opts.txCacheSize=100]
*/
function Verified(network, opts) {
var self = this
Blockchain.call(self, network, opts)

opts = _.extend({
isTestnet: false,
compactMode: false,
testnet: false,
useCompactMode: false,
usePreSavedChunkHashes: false,
headerCacheSize: 10000,
txCacheSize: 100
}, opts)

yatc.verify('Network', network)
yatc.create([
if (network.getNetworkName() !== self.getNetworkName()) {
throw new TypeError('Network and Blockchain have different networks')
}
yatc.verify([
'{',
'storage: Storage,',
'isTestnet: Boolean,',
'compactMode: Boolean,',
'testnet: Boolean,',
'useCompactMode: Boolean,',
'usePreSavedChunkHashes: Boolean,',
'headerCacheSize: PositiveNumber|ZeroNumber,',
'txCacheSize: PositiveNumber|ZeroNumber',
'txCacheSize: PositiveNumber|ZeroNumber,',
'...',
'}'
].join(''), opts)
if (opts.compactMode !== opts.storage.isUsedCompactMode()) {
throw new TypeError('Storage compactMode not compatible with Blockchain compactMode')
if (opts.useCompactMode !== opts.storage.isUsedCompactMode()) {
throw new TypeError('Storage and Blockchain have different compactMode')
}

var self = this
Blockchain.call(self, network)

// save storage (opts.compactMode not needed because already yet in storage)
self._storage = opts.storage

// save testnet mode, needed for header verification
self._isTestnet = opts.isTestnet
self._isTestnet = opts.testnet

// create header and tx caches
self._headerCache = LRU({max: opts.headerCacheSize})
Expand Down Expand Up @@ -262,7 +266,7 @@ function Verified(network, opts) {
self._storage.once('ready', resolve)
})
.then(function () {
return self._initialize({usePreSavedChunkHashes: opts.usePreSavedChunkHashes})
return self._initialize(opts)
})
.then(function () {
// set isReady is true and start syncing after initialization
Expand All @@ -280,10 +284,10 @@ inherits(Verified, Blockchain)
*
* @param {Object} opts
* @param {boolean} opts.usePreSavedChunkHashes
* @return {Q.Promise}
* @return {Promise}
*/
Verified.prototype._initialize = function (opts) {
yatc.verify('{usePreSavedChunkHashes: Boolean}', opts)
yatc.verify('{usePreSavedChunkHashes: Boolean, ...}', opts)

var self = this
var storage = self._storage
Expand Down Expand Up @@ -334,7 +338,7 @@ Verified.prototype._initialize = function (opts) {
}

/**
* @return {Q.Promise}
* @return {Promise}
*/
Verified.prototype._sync = function () {
var self = this
Expand Down Expand Up @@ -659,7 +663,7 @@ Verified.prototype.isSyncing = function () {
* @param {number} height
* @param {boolean} [waitHeader=true]
* wait header if height greather than current blockchain height
* @return {Q.Promise<BitcoinHeader>}
* @return {Promise<BitcoinHeader>}
*/
Verified.prototype.getHeader = function (height, waitHeader) {
if (typeof waitHeader === 'undefined') {
Expand Down Expand Up @@ -752,7 +756,7 @@ Verified.prototype.getHeader = function (height, waitHeader) {

/**
* @param {string} txId
* @return {Q.Promise<string>}
* @return {Promise<string>}
*/
Verified.prototype.getTx = function (txId) {
var self = this
Expand Down Expand Up @@ -820,31 +824,31 @@ Verified.prototype.getTx = function (txId) {

/**
* @param {string} txHex
* @return {Q.Promise<string>}
* @return {Promise<string>}
*/
Verified.prototype.sendTx = function (txHex) {
return this.network.sendTx(txHex)
}

/**
* @param {string} address
* @return {Q.Promise<Network~HistoryObject>}
* @return {Promise<Network~HistoryObject>}
*/
Verified.prototype.getHistory = function (address) {
return this.network.getHistory(address)
}

/**
* @param {string} address
* @return {Q.Promise<Network~UnspentObject>}
* @return {Promise<Network~UnspentObject>}
*/
Verified.prototype.getUnspent = function (address) {
return this.network.getUnspent(address)
}

/**
* @param {string} address
* @return {Q.Promise}
* @return {Promise}
*/
Verified.prototype.subscribeAddress = function (address) {
return this.network.subscribeAddress(address)
Expand Down
Loading

0 comments on commit 2cd0f92

Please sign in to comment.