Skip to content

Commit

Permalink
fix newHeight event, add txStatus.dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Nov 21, 2014
1 parent c4c630b commit a6ade0d
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cc-wallet-core",
"version": "0.2.1",
"version": "0.2.2",
"description": "",
"main": "./src/index.js",
"keywords": [],
Expand Down
7 changes: 6 additions & 1 deletion src/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ var verify = require('./verify')
* @param {Object} [opts.networkOpts]
* @param {string} [opts.blockchain=VerifiedBlockchain]
* @param {number} [opts.storageSaveTimeout=1000] In milliseconds
* @param {boolean} [opts.spendUnconfirmedCoins=false]
*/
function Wallet(opts) {
opts = _.extend({
testnet: false,
network: 'Electrum',
blockchain: 'VerifiedBlockchain',
storageSaveTimeout: 1000
storageSaveTimeout: 1000,
spendUnconfirmedCoins: false
}, opts)

var self = this
Expand All @@ -100,8 +102,10 @@ function Wallet(opts) {
verify.boolean(opts.networkOpts.testnet)
verify.string(opts.blockchain)
verify.number(opts.storageSaveTimeout)
verify.boolean(opts.spendUnconfirmedCoins)

self.bitcoinNetwork = opts.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin
self._spendUnconfirmedCoins = opts.spendUnconfirmedCoins

self.config = new ConfigStorage()

Expand Down Expand Up @@ -156,6 +160,7 @@ function Wallet(opts) {
inherits(Wallet, events.EventEmitter)

Wallet.prototype.getBitcoinNetwork = function() { return this.bitcoinNetwork }
Wallet.prototype.canSpendUnconfirmedCoins = function() { return this._spendUnconfirmedCoins }
Wallet.prototype.getNetwork = function() { return this.network }
Wallet.prototype.getBlockchain = function() { return this.blockchain }
Wallet.prototype.getColorDefinitionManager = function() { return this.cdManager }
Expand Down
5 changes: 2 additions & 3 deletions src/blockchain/VerifiedBlockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function VerifiedBlockchain(network, opts) {

if (running) {
queue.push(Q.defer())
promise = _.last(queue)
promise = _.last(queue).promise
}
running = true

Expand Down Expand Up @@ -347,8 +347,7 @@ VerifiedBlockchain.prototype._sync = function() {
var networkLastHash = self._network.getCurrentBlockHash()

if (bufferEqual(self._currentBlockHash, networkLastHash) || networkHeight === -1) {
deferred.resolve()
return deferred.promise
return Q()
}

var maxBits = 0x1d00ffff
Expand Down
15 changes: 8 additions & 7 deletions src/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
* @enum {number}
*/
var txStatus = {
unknown: 0,
unconfirmed: 1,
confirmed: 2,
invalid: 3,
pending: 4
unknown: 0, // Unknown status :-)
unconfirmed: 1, // As pending only transaction was pushed not from us
confirmed: 2, // Transaction in blockchain
invalid: 3, // Double-spend, can't be accepted by network and others cases...
pending: 4, // Network accepted our transaction but not include in blockchain yet
dispatch: 5 // Transaction must be sent to network
}

txStatus.valid = [txStatus.unconfirmed, txStatus.confirmed, txStatus.pending]
txStatus.valid = [txStatus.unconfirmed, txStatus.confirmed, txStatus.pending, txStatus.dispatch]
/**
* @param {number} status
* @return {boolean}
Expand All @@ -19,7 +20,7 @@ txStatus.isValid = function(status) {
return txStatus.valid.indexOf(status) !== -1
}

txStatus.available = [txStatus.confirmed, txStatus.pending]
txStatus.available = [txStatus.confirmed, txStatus.pending, txStatus.dispatch]
/**
* @param {number} status
* @return {boolean}
Expand Down
2 changes: 1 addition & 1 deletion src/network/Electrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function Electrum(wallet, opts) {

if (response.id === null) {
var isMethod = response.method === 'blockchain.numblocks.subscribe'
var isArgs = _.isNumber(response.params) && _.isNumber(response.params[0])
var isArgs = _.isArray(response.params) && _.isNumber(response.params[0])
if (isMethod && isArgs)
return self._setCurrentHeight(response.params[0])

Expand Down
2 changes: 1 addition & 1 deletion src/network/Network.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Network.prototype._setCurrentHeight = function(newHeight) {
var promise = Q()
if (self._setCurrentHeightRunning === true) {
self._setCurrentHeightQueue.push(Q.defer())
promise = _.last(self._setCurrentHeightQueue)
promise = _.last(self._setCurrentHeightQueue).promise
}
self._setCurrentHeightRunning = true

Expand Down
3 changes: 2 additions & 1 deletion src/tx/OperationalTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ OperationalTx.prototype.selectCoins = function(colorValue, feeEstimator, cb) {
var coinQuery = self.wallet.getCoinQuery()
coinQuery = coinQuery.onlyColoredAs(colordef)
coinQuery = coinQuery.onlyAddresses(self.wallet.getAllAddresses(colordef))
coinQuery = coinQuery.includeUnconfirmed()
if (self.wallet.canSpendUnconfirmedCoins())
coinQuery = coinQuery.includeUnconfirmed()

return Q.ninvoke(coinQuery, 'getCoins')

Expand Down
28 changes: 20 additions & 8 deletions src/tx/TxDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function TxDb(wallet, txStorage) {

self._txStorage.getAllTxIds().forEach(function(txId) {
var record = self._txStorage.get(txId)
if (record !== null && record.status === txStatus.pending)
if (record !== null && record.status === txStatus.dispatch)
process.nextTick(function() { self._attemptSendTx(txId) })
})
}
Expand All @@ -87,20 +87,29 @@ TxDb.prototype._attemptSendTx = function(txId, attempt) {
if (record === null)
return

// No needed change status: on success, historySync do it
/**
* @param {number} status
*/
function updateTx(status) {
self._txStorage.update(txId, {status: status})
self.emit('updateTx', tx)
}

var tx = Transaction.fromHex(record.rawTx)
Q.ninvoke(self._wallet.getBlockchain(), 'sendTx', tx).catch(function(error) {
Q.ninvoke(self._wallet.getBlockchain(), 'sendTx', tx).done(function () {
updateTx(txStatus.pending)

}, function(error) {
self.emit('error', error)

if (attempt >= 5) {
self._txStorage.update(txId, {status: txStatus.invalid})
return self.emit('updateTx', tx)
return updateTx(txStatus.invalid)
}

var timeout = 15000 * Math.pow(2, attempt)
Q.delay(timeout).then(function() { self._attemptSendTx(txId, attempt + 1) })

}).done()
})
}

/**
Expand Down Expand Up @@ -129,7 +138,6 @@ TxDb.prototype._addTx = function(txId, data, cb) {
verify.function(cb)

if (data.tx) data.rawTx = data.tx.toHex()
if (_.isUndefined(data.status)) data.status = txStatus.unknown

var self = this

Expand Down Expand Up @@ -174,6 +182,10 @@ TxDb.prototype._addTx = function(txId, data, cb) {
}

}).then(function() {
if (_.isUndefined(data.status)) data.status = txStatus.unknown
if (record !== null && record.status === txStatus.pending && data.status === txStatus.unconfirmed)
data.status = txStatus.pending

if (_.isUndefined(data.tAddresses)) data.tAddresses = []
if (record !== null)
data.tAddresses = _.union(data.tAddresses, record.tAddresses)
Expand Down Expand Up @@ -231,7 +243,7 @@ TxDb.prototype.sendTx = function(tx, cb) {
var addTxOpts = {
height: 0,
tx: tx,
status: txStatus.pending,
status: txStatus.dispatch,
timestamp: getCurrentTimestamp(),
tAddresses: []
}
Expand Down
18 changes: 12 additions & 6 deletions test/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ describe('Wallet', function() {

function setup() {
localStorage.clear()
wallet = new Wallet({ testnet: true, blockchain: 'NaiveBlockchain', storageSaveTimeout: 0 })
wallet = new Wallet({
testnet: true,
blockchain: 'NaiveBlockchain',
storageSaveTimeout: 0,
spendUnconfirmedCoins: true
})
}

function cleanup() {
Expand Down Expand Up @@ -224,14 +229,14 @@ describe('Wallet', function() {
var deferred = Q.defer()
deferred.promise.done(done, done)

var seed = '421fc385fdae762b346b80e0212f77bb'
var seed = '421fc385fdae762b246b80e0212f77bb'
wallet.initialize(seed)
wallet.addAssetDefinition(seed, goldAsset)
wallet.subscribeAndSyncAllAddresses(function(error) {
expect(error).to.be.null

var bitcoin = wallet.getAssetDefinitionByMoniker('bitcoin')
var targets = [{ address: 'mo8Ni5kFSxcuEVXbfBaSaDzMiq1j4E6wUE', value: 10000 }]
var targets = [{ address: 'miFxiMoU4AmybucyCM5sXB8mPUVpEtP5E1', value: 10000 }]

wallet.createTx(bitcoin, targets, function(error, tx) {
expect(error).to.be.null
Expand All @@ -258,7 +263,8 @@ describe('Wallet', function() {
masterKey: '421fc385fdae762b346b80e0212f77bd',
testnet: true,
blockchain: 'NaiveBlockchain',
storageSaveTimeout: 0
storageSaveTimeout: 0,
spendUnconfirmedCoins: true
})

var data = {
Expand Down Expand Up @@ -297,13 +303,13 @@ describe('Wallet', function() {
})

it('issueCoins epobc', function(done) {
var seed = '421fc385fdaed1121221222eddad0dae'
var seed = '421ac385fdaed1121321222eddad0dae'
wallet.initialize(seed)
wallet.addAssetDefinition(seed, goldAsset)
wallet.subscribeAndSyncAllAddresses(function(error) {
expect(error).to.be.null

wallet.createIssuanceTx('newEPOBC', 'epobc', 5, 10000, seed, function(error, tx) {
wallet.createIssuanceTx('newEPOBC', 'epobc', 2, 10000, seed, function(error, tx) {
expect(error).to.be.null

wallet.transformTx(tx, 'signed', seed, function(error, tx) {
Expand Down
6 changes: 5 additions & 1 deletion test/blockchain.VerifiedBlockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ describe('blockchain.VerifiedBlockchain', function() {
var wallet

beforeEach(function() {
wallet = new Wallet({ testnet: true, storageSaveTimeout: 0 })
wallet = new Wallet({
testnet: true,
storageSaveTimeout: 0,
spendUnconfirmedCoins: true
})
})

afterEach(function() {
Expand Down
3 changes: 2 additions & 1 deletion test/network.implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function networkImplementationTest(opts) {
testnet: true,
network: opts.class.name,
blockchain: 'NaiveBlockchain',
storageSaveTimeout: 0
storageSaveTimeout: 0,
spendUnconfirmedCoins: true
})
network = wallet.network
network.once('connect', done)
Expand Down
7 changes: 6 additions & 1 deletion test/tx.TxFetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ describe('tx.TxFetcher', function() {
var wallet, txFetcher, addresses

beforeEach(function() {
wallet = new Wallet({ testnet: true, blockchain: 'NaiveBlockchain', storageSaveTimeout: 0 })
wallet = new Wallet({
testnet: true,
blockchain: 'NaiveBlockchain',
storageSaveTimeout: 0,
spendUnconfirmedCoins: true
})
wallet.initialize('123131123131123131123131123131123131123131123131123131')

txFetcher = wallet.getTxFetcher()
Expand Down

0 comments on commit a6ade0d

Please sign in to comment.