Skip to content

Commit

Permalink
change network.getTx
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Dec 21, 2014
1 parent 3affa4d commit 6bd7b18
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 59 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@ before_install:
node_js:
- "0.11"
- "0.10"
env:
- TEST_SUITE=jshint jscs compile
- TEST_SUITE=coveralls
script: "grunt $TEST_SUITE"
script: "grunt jshint jscs compile coveralls"
1 change: 1 addition & 0 deletions src/blockchain/Blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Blockchain.prototype.getBlockTime = function () {
/**
* @abstract
* @param {string} txId
* @param {WalletState} [walletState]
* @param {Blockchain~getTx} cb
*/
Blockchain.prototype.getTx = function () {
Expand Down
11 changes: 8 additions & 3 deletions src/blockchain/NaiveBlockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var inherits = require('util').inherits

var _ = require('lodash')
var LRU = require('lru-cache')
var Q = require('q')

var verify = require('../verify')
var Blockchain = require('./Blockchain')
Expand Down Expand Up @@ -84,8 +83,14 @@ NaiveBlockchain.prototype.getBlockTime = function (height, cb) {
/**
* {@link Blockchain~getTx}
*/
NaiveBlockchain.prototype.getTx = function (txId, cb) {
NaiveBlockchain.prototype.getTx = function (txId, walletState, cb) {
if (_.isFunction(walletState) && _.isUndefined(cb)) {
cb = walletState
walletState = undefined
}

verify.txId(txId)
if (!_.isUndefined(walletState)) { verify.WalletState(walletState) }
verify.function(cb)

var self = this
Expand All @@ -96,7 +101,7 @@ NaiveBlockchain.prototype.getTx = function (txId, cb) {
}

if (_.isUndefined(self._getTxRunning[txId])) {
self._getTxRunning[txId] = self._network.getTx(txId).then(function (tx) {
self._getTxRunning[txId] = self._network.getTx(txId, walletState).then(function (tx) {
self._txCache.set(txId, tx)
return tx

Expand Down
20 changes: 14 additions & 6 deletions src/blockchain/VerifiedBlockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,16 @@ VerifiedBlockchain.prototype.getBlockTime = function (height, cb) {
/**
* {@link Blockchain~getTx}
*/
VerifiedBlockchain.prototype.getTx = function (txId, cb) {
VerifiedBlockchain.prototype.getTx = function (txId, walletState, cb) {
if (_.isFunction(walletState) && _.isUndefined(cb)) {
cb = walletState
walletState = undefined
}

if (!_.isUndefined(walletState)) { verify.WalletState(walletState) }
verify.function(cb)

this._getVerifiedTx(txId)
this._getVerifiedTx(txId, walletState)
.done(function (tx) { cb(null, tx) }, function (error) { cb(error) })
}

Expand Down Expand Up @@ -257,17 +263,19 @@ VerifiedBlockchain.prototype._getVerifiedHeader = function (height) {

/**
* @param {string} txId
* @param {WalletState} [walletState]
* @return {Q.Promise<Transaction>}
*/
VerifiedBlockchain.prototype._getTx = function (txId) {
return this._network.getTx(txId)
VerifiedBlockchain.prototype._getTx = function (txId, walletState) {
return this._network.getTx(txId, walletState)
}

/**
* @param {string} txId
* @param {WalletState} [walletState]
* @return {Q.Promise<Transaction>}
*/
VerifiedBlockchain.prototype._getVerifiedTx = function (txId) {
VerifiedBlockchain.prototype._getVerifiedTx = function (txId, walletState) {
verify.txId(txId)

var self = this
Expand All @@ -282,7 +290,7 @@ VerifiedBlockchain.prototype._getVerifiedTx = function (txId) {
var height
var merkleRoot

self._getVerifiedTxRunning[txId] = self._getTx(txId).then(function (result) {
self._getVerifiedTxRunning[txId] = self._getTx(txId, walletState).then(function (result) {
tx = result
return self._network.getMerkle(txId).catch(function (error) {
if (error.message === 'BlockNotFound') {
Expand Down
28 changes: 15 additions & 13 deletions src/coin/CoinManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ var verify = require('../verify')
* @class CoinManager
* @extends events.EventEmitter
* @param {Wallet} wallet
* @param {TxManager} txManager
* @param {WalletState} walletState
* @param {Object} rawStorage
*/
function CoinManager(wallet, txManager, rawStorage) {
function CoinManager(wallet, walletState, rawStorage) {
verify.Wallet(wallet)
verify.TxManager(txManager)
verify.WalletState(walletState)
verify.object(rawStorage)
rawStorage = _.defaults(rawStorage, {'coins': [], 'spend': {}})

events.EventEmitter.call(this)

this._wallet = wallet
this._txManager = txManager
this._walletState = walletState
this._coins = rawStorage.coins
this._spend = rawStorage.spend
}
Expand All @@ -49,7 +49,7 @@ inherits(CoinManager, events.EventEmitter)
* @return {Coin}
*/
CoinManager.prototype._record2Coin = function (record) {
var txData = this._txManager.getTxData(record.txId)
var txData = this._walletState.getTxManager().getTxData(record.txId)
if (txData === null) {
// @todo Throw custom error
throw new Error('TxNotFound: ' + record.txId)
Expand Down Expand Up @@ -219,7 +219,7 @@ CoinManager.prototype.isCoinSpent = function (coin) {
CoinManager.prototype.isCoinValid = function (coin) {
verify.Coin(coin)

var txData = this._txManager.getTxData(coin.txId)
var txData = this._walletState.getTxManager().getTxData(coin.txId)
return txData !== null && txStatus.isValid(txData.status)
}

Expand All @@ -230,7 +230,7 @@ CoinManager.prototype.isCoinValid = function (coin) {
CoinManager.prototype.isCoinAvailable = function (coin) {
verify.Coin(coin)

var txData = this._txManager.getTxData(coin.txId)
var txData = this._walletState.getTxManager().getTxData(coin.txId)
return txData !== null && txStatus.isAvailable(txData.status)
}

Expand Down Expand Up @@ -302,14 +302,16 @@ CoinManager.prototype.getCoinMainColorValue = function (coin, cb) {
* @return {Q.Promise<ColorValue[]>}
*/
CoinManager.prototype.getTxMainColorValues = function (tx) {
var colorDefinitions = this._wallet.getColorDefinitionManager().getAllColorDefinitions()
var colorData = this._wallet.getColorData()
var getTxColorValues = Q.nbind(colorData.getTxColorValues, colorData)
var blockchain = this._wallet.getBlockchain()
var getTxFn = blockchain.getTx.bind(blockchain)
var self = this

var colorDefinitions = self._wallet.getColorDefinitionManager().getAllColorDefinitions()
return Q.all(colorDefinitions.map(function (colorDefinition) {
return getTxColorValues(tx, colorDefinition, getTxFn)
var blockchain = self._wallet.getBlockchain()
function getTxFn(txId, cb) {
blockchain.getTx(txId, self._walletState, cb)
}

return Q.ninvoke(self._wallet.getColorData(), 'getTxColorValues', tx, colorDefinition, getTxFn)

})).then(function (colorValuess) {
var nullColorValues = Array.apply(null, Array(tx.outs.length)).map(function () { return null })
Expand Down
28 changes: 12 additions & 16 deletions src/history/HistoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ var historyEntryType = require('../const').historyEntryType

/**
* @param {{txId1: string, txIdN:string}[]} entries
* @param {function} getTxFn
* @param {TxManager} txManager
* @return {{txId1: string, txIdN:string}[]}
*/
function toposort(entries, getTxFn) {
function toposort(entries, txManager) {
var entriesTxIds = _.zipObject(entries.map(function (entry) { return [entry.txId, entry] }))
var result = []
var resultTxIds = {}
Expand All @@ -29,7 +29,7 @@ function toposort(entries, getTxFn) {
return
}

getTxFn(entry.txId).ins.forEach(function (input) {
txManager.getTx(entry.txId).ins.forEach(function (input) {
var inputId = Array.prototype.reverse.call(new Buffer(input.hash)).toString('hex')
if (_.isUndefined(entriesTxIds[inputId])) {
return
Expand Down Expand Up @@ -60,22 +60,19 @@ function toposort(entries, getTxFn) {
* @class HistoryManager
* @extends events.EventEmitter
* @param {Wallet} wallet
* @param {TxManager} txManager
* @param {CoinManager} coinManager
* @param {WalletState} walletState
* @param {Object} rawStorage
*/
function HistoryManager(wallet, txManager, coinManager, rawStorage) {
function HistoryManager(wallet, walletState, rawStorage) {
verify.Wallet(wallet)
verify.TxManager(txManager)
verify.CoinManager(coinManager)
verify.WalletState(walletState)
verify.object(rawStorage)

var self = this
events.EventEmitter.call(self)

self._wallet = wallet
self._txManager = txManager
self._coinManager = coinManager
self._walletState = walletState
self._historyRecords = rawStorage
}

Expand All @@ -87,16 +84,15 @@ HistoryManager.prototype._resortHistoryRecords = function () {
var self = this

var orderedRecords = _.sortBy(self._historyRecords, function (record) {
var txData = self._txManager.getTxData(record.txId)
var txData = self._walletState.getTxManager().getTxData(record.txId)
if (txData.height === 0) {
return txData.timestamp
}

return txData.height + txData.timestamp / 10000000000
})

var getTxFn = self._txManager.getTx.bind(self._txManager)
toposort(orderedRecords, getTxFn).forEach(function (record, index) {
toposort(orderedRecords, self._walletState.getTxManager()).forEach(function (record, index) {
self._historyRecords[index] = record
})
}
Expand Down Expand Up @@ -133,7 +129,7 @@ HistoryManager.prototype.addTx = function (tx) {

myInsCount += 1

var coin = new Coin(self._coinManager, {
var coin = new Coin(self._walletState.getCoinManager(), {
txId: input.prevTx.getId(),
outIndex: input.index,
value: input.prevTx.outs[input.index].value,
Expand All @@ -154,7 +150,7 @@ HistoryManager.prototype.addTx = function (tx) {
}))

}).then(function () {
return self._coinManager.getTxMainColorValues(tx).then(function (txColorValues) {
return self._walletState.getCoinManager().getTxMainColorValues(tx).then(function (txColorValues) {
txColorValues.forEach(function (colorValue, index) {
var colorTarget = new cclib.ColorTarget(tx.outs[index].script.toHex(), colorValue)

Expand Down Expand Up @@ -243,7 +239,7 @@ HistoryManager.prototype.revertTx = function (tx) {
HistoryManager.prototype.getEntries = function (assetdef) {
var assetDefinitionManager = this._wallet.getAssetDefinitionManager()
var bitcoinNetwork = this._wallet.getBitcoinNetwork()
var txManager = this._txManager
var txManager = this._walletState.getTxManager()

var assetId = null
if (!_.isUndefined(assetdef)) {
Expand Down
4 changes: 2 additions & 2 deletions src/network/Chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var Network = require('./Network')
* @param {number} [opts.refreshInterval=30*1000]
*/
function Chain(wallet, opts) {
// @todo Change to Promises
throw new Error('Not supported right now')
// @todo Change to Promises
throw new Error('Not supported right now')

verify.Wallet(wallet)
opts = _.extend({
Expand Down
10 changes: 3 additions & 7 deletions src/network/Electrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ Electrum.prototype.getChunk = function (index) {
/**
* {@link Network~getTx}
*/
Electrum.prototype.getTx = function (txId) {
Electrum.prototype.getTx = function (txId, walletState) {
verify.txId(txId)
if (!_.isUndefined(walletState)) { verify.WalletState(walletState) }

var tx = this._wallet.getStateManager().getTx(txId)
var tx = this._wallet.getStateManager().getTx(txId, walletState)
if (tx !== null) {
return Q(tx)
}
Expand All @@ -193,11 +194,6 @@ Electrum.prototype.getTx = function (txId) {
* {@link Network~getMerkle}
*/
Electrum.prototype.getMerkle = function (txId, height) {
if (_.isFunction(height) && _.isUndefined(cb)) {
cb = height
height = undefined
}

verify.txId(txId)
if (!_.isUndefined(height)) { verify.number(height) }

Expand Down
4 changes: 3 additions & 1 deletion src/tx/TxManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ function getCurrentTimestamp() {
* @extends events.EventEmitter
* @mixes SyncMixin
* @param {Wallet} wallet
* @param {WalletState} walletState
* @param {Object} rawStorage
*/
function TxManager(wallet, rawStorage) {
function TxManager(wallet, walletState, rawStorage) {
verify.Wallet(wallet)
verify.WalletState(walletState)
verify.object(rawStorage)

events.EventEmitter.call(this)
Expand Down
3 changes: 2 additions & 1 deletion src/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ var functions = {
Blockchain: createInstanceCheck(function () { return require('./blockchain/Blockchain') }),
NaiveBlockchain: createInstanceCheck(function () { return require('./blockchain/NaiveBlockchain') }),
VerifiedBlockchain: createInstanceCheck(function () { return require('./blockchain/VerifiedBlockchain') }),
VerifiedBlockchainStorage: createInstanceCheck(function () { return require('./blockchain/VerifiedBlockchainStorage') }),
VerifiedBlockchainStorage: createInstanceCheck(
function () { return require('./blockchain/VerifiedBlockchainStorage') }),

rawCoin: isRawCoin,
Coin: createInstanceCheck(function () { return require('./coin/Coin') }),
Expand Down
6 changes: 3 additions & 3 deletions src/wallet/WalletState.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ function WalletState(wallet) {
this.store.set(this._stateDBKey + '_version', 1)
}

this._txManager = new TxManager(wallet, this._state.tx)
this._coinManager = new CoinManager(wallet, this._txManager, this._state.coin)
this._historyManager = new HistoryManager(wallet, this._txManager, this._coinManager, this._state.history)
this._txManager = new TxManager(wallet, this, this._state.tx)
this._coinManager = new CoinManager(wallet, this, this._state.coin)
this._historyManager = new HistoryManager(wallet, this, this._state.history)
}

inherits(WalletState, SyncStorage)
Expand Down
8 changes: 6 additions & 2 deletions src/wallet/WalletStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,14 @@ WalletStateManager.prototype.sendTx = function (tx) {

/**
* @param {string} txId
* @param {WalletState} [walletState=currentWalletState]
* @return {?Transaction}
*/
WalletStateManager.prototype.getTx = function (txId) {
return this._currentState.getTxManager().getTx(txId)
WalletStateManager.prototype.getTx = function (txId, walletState) {
walletState = _.isUndefined(walletState) ? this._currentState : walletState
verify.WalletState(walletState)

return walletState.getTxManager().getTx(txId)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/network.implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function networkImplementationTest(opts) {
spendUnconfirmedCoins: true
})
network = wallet.network
network.on('error', function(error) { throw error })
network.on('error', function (error) { throw error })
network.once('connect', done)
})

Expand Down

0 comments on commit 6bd7b18

Please sign in to comment.